forked from justinmok/wtwcomms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.c
146 lines (118 loc) · 4.19 KB
/
host.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <kipr/botball.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* File Name : main.c
* Last Modified Date : 09/12/2018 (mm/dd/yyyy)
* Purpose : Wallaby-to-Wallaby Communication (THIS IS FOR THE HOST**************************88)
* Author(s) : Cini (Justin Mok)
* Notes :
*/
/*
function call sequence:
HOST initHost();
CLIENT connectWifi();
CLIENT initSocket();
HOST sendData();
CLIENT listenData();
*/
struct hostent *host_ent;
struct sockaddr_in host;
struct sockaddr_in dest;
int hostSocket, clientSocket;
int data;
socklen_t size = sizeof(struct sockaddr_in);
thread listener;
void initHostWifi() {
system("wpa_cli ter");
system("killall hostapd");
system("wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant-wlan0.conf");
system("/usr/bin/python /usr/bin/wifi_configurator.py");
printf("host wifi initialized\n");
}
void listenData() {
while (1) {
int received = recv(hostSocket, (int *)data, 999, 0);
if (received == 0 && received == -1) {
printf("error receiving socket data");
} else {
printf("read data: %i", data);
} // error handling
}
};
void initHost() {
initHostWifi();
int hostSocket = socket(AF_INET, SOCK_STREAM, 0);
struct hostent *serverStruct = gethostbyname("192.168.125.1");
host.sin_family = AF_INET;
host.sin_port = htons(8001);
bcopy((char *)serverStruct->h_addr, (char *)&host.sin_addr.s_addr, serverStruct->h_length);
bind(hostSocket, (struct sockaddr *)&host, sizeof(struct sockaddr));
printf("Initialized Socket Connection");
listen(hostSocket, 1);
printf("Started Listening on port 8001");
clientSocket = accept(hostSocket, (struct sockaddr *)&dest, &size);
if (clientSocket == -1) {
printf("Socket Connection Failed");
}
printf("Socket Connection Success");
listener = thread_create(listenData);
}
void connectWifi() {
/*********************ONLY RUN IF BRAIN IS CLIENT // YOU WILL NEED TO RESTART WALLABY*********************/
// Initialize wpa_cli
system("wpa_cli ter"); // initializes wpa_cli
// Kills WiFi host
system("killall hostapd");
system("echo Killed Wifi");
// Initialize wpa_supplicant config file in order to use wpa_cli
system("wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant-wlan0.conf"); // init wpa supplicant for wpa_cli
system("echo initialized wpa_supplicant");
// Remove existing networks on wpa_cli
system("wpa_cli remove_network 0"); //
// Create network, pass network SSID and password, enable network, and save network config
system("wpa_cli add_network"); // init network creation
system("wpa_cli set_network 0 ssid \'\"1904-wallaby\"\'");
system("wpa_cli set_network 0 psk \'\"90bbc900\"\'");
system("wpa_cli enable_network 0");
system("wpa_cli save");
system("echo saved and connected to network");
}
void initSocket() {
listener = thread_create(listenData);
/********************* THIS IS FOR CLIENT *********************/
struct hostent *serverStruct;
int clientSocket;
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8001);
// &server_addr.sin_addr.s_addr = "192.168.125.1"; idk why this doesnt work
serverStruct = gethostbyname("192.168.125.1");
bcopy((char *)serverStruct->h_addr, (char *)&server_addr.sin_addr.s_addr, serverStruct->h_length); // copy string bytes from h_addr to s_addr
if (connect(clientSocket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0) {
printf("Connection failed.\n");
} else {
printf("Connected Successful.\n");
}
}
void sendData(int data) {
int packet = send(clientSocket, (int *) data, 999, 0);
if (packet == -1) {
printf("sending data failed");
}
}
int main() {
printf("Hello World!\n");
connectWifi();
printf("Goodbye World!\n");
return 0;
}