| 75 | | char token[64]; |
|---|
| 76 | | char username[64]; |
|---|
| 77 | | char xmpp_file_name[128]; |
|---|
| 78 | | char sampl_file_name[128]; |
|---|
| 79 | | char password[64]; |
|---|
| 80 | | char xmpp_server[64]; |
|---|
| 81 | | char xmpp_ssl_fingerprint[64]; |
|---|
| 82 | | char pubsub_server[64]; |
|---|
| 83 | | uint32_t xmpp_server_port; |
|---|
| 84 | | |
|---|
| 85 | | |
|---|
| 86 | | debug_txt_flag=0; |
|---|
| 87 | | xmpp_flag=0; |
|---|
| 88 | | no_slip_flag=0; |
|---|
| 89 | | print_input_flag=0; |
|---|
| 90 | | connection=NULL; |
|---|
| 91 | | |
|---|
| 92 | | if (argc < 3 || argc > 6) { |
|---|
| 93 | | printf ("Usage: server port [-vxnf] [xmmp_config_name] [sampl_config_name]\n"); |
|---|
| 94 | | printf (" v Print Verbose Debug\n"); |
|---|
| 95 | | printf (" x Send data to XMPP server\n"); |
|---|
| 96 | | printf (" n Don't send SLIP packets (but receive them)\n"); |
|---|
| 97 | | printf (" f Use the following config files instead of the defaults\n"); |
|---|
| 98 | | exit (1); |
|---|
| 99 | | } |
|---|
| 100 | | |
|---|
| 101 | | strcpy( xmpp_file_name, "xmpp_config.txt" ); |
|---|
| 102 | | strcpy( sampl_file_name, "ff_config.txt" ); |
|---|
| 103 | | if(argc>3) |
|---|
| 104 | | { |
|---|
| 105 | | // Grab dash command line options |
|---|
| 106 | | if(strstr(argv[3],"v")!=NULL ) |
|---|
| 107 | | { |
|---|
| 108 | | printf( "Verbose Mode ON\n" ); |
|---|
| 109 | | debug_txt_flag=1; |
|---|
| 110 | | } |
|---|
| 111 | | if(strstr(argv[3],"x")!=NULL ) |
|---|
| 112 | | { |
|---|
| 113 | | printf( "XMPP ON\n" ); |
|---|
| 114 | | xmpp_flag=1; |
|---|
| 115 | | } |
|---|
| 116 | | if(strstr(argv[3],"n")!=NULL ) |
|---|
| 117 | | { |
|---|
| 118 | | printf( "SLIP TX OFF\n" ); |
|---|
| 119 | | no_slip_flag=1; |
|---|
| 120 | | } |
|---|
| 121 | | if(strstr(argv[3],"f")!=NULL ) |
|---|
| 122 | | { |
|---|
| 123 | | printf( "Loading XMPP config: " ); |
|---|
| 124 | | strcpy(xmpp_file_name, argv[4]); |
|---|
| 125 | | printf( "%s\n",xmpp_file_name ); |
|---|
| 126 | | printf( "Loading SAMPL config: " ); |
|---|
| 127 | | strcpy(sampl_file_name, argv[5]); |
|---|
| 128 | | printf( "%s\n",sampl_file_name ); |
|---|
| 129 | | } |
|---|
| 130 | | } |
|---|
| 131 | | |
|---|
| 132 | | if(xmpp_flag) |
|---|
| 133 | | { |
|---|
| 134 | | fp=fopen( xmpp_file_name,"r" ); |
|---|
| 135 | | if(fp==NULL) |
|---|
| 136 | | { |
|---|
| 137 | | printf( "XMPP config: No %s file!\n",xmpp_file_name ); |
|---|
| 138 | | exit(0); |
|---|
| 139 | | } |
|---|
| 140 | | param=0; |
|---|
| 141 | | do { |
|---|
| 142 | | v=fscanf( fp, "%[^\n]\n", buf); |
|---|
| 143 | | if(buf[0]!='#' && v!=-1) |
|---|
| 144 | | { |
|---|
| 145 | | switch(param) |
|---|
| 146 | | { |
|---|
| 147 | | case 0: strcpy(username,buf); break; |
|---|
| 148 | | case 1: strcpy(password,buf); break; |
|---|
| 149 | | case 2: strcpy(xmpp_server,buf); break; |
|---|
| 150 | | case 3: xmpp_server_port=atoi(buf); break; |
|---|
| 151 | | case 4: strcpy(pubsub_server,buf); break; |
|---|
| 152 | | case 5: strcpy(xmpp_ssl_fingerprint,buf); break; |
|---|
| 153 | | } |
|---|
| 154 | | param++; |
|---|
| 155 | | } |
|---|
| 156 | | |
|---|
| 157 | | }while(v!=-1 && param<6 ); |
|---|
| 158 | | if(debug_txt_flag) |
|---|
| 159 | | { |
|---|
| 160 | | printf( "XMPP Client Configuration:\n" ); |
|---|
| 161 | | printf( " username: %s\n",username ); |
|---|
| 162 | | printf( " password: %s\n",password); |
|---|
| 163 | | printf( " xmpp server: %s\n",xmpp_server); |
|---|
| 164 | | printf( " xmpp server port: %d\n",xmpp_server_port); |
|---|
| 165 | | printf( " xmpp pubsub server: %s\n",pubsub_server); |
|---|
| 166 | | printf( " xmpp ssl fingerprint: %s\n\n",xmpp_ssl_fingerprint); |
|---|
| 167 | | } |
|---|
| 168 | | if(param<4) |
|---|
| 169 | | { |
|---|
| 170 | | printf( "Not enough xmpp configuration parameters in xmpp_config.txt\n" ); |
|---|
| 171 | | exit(0); |
|---|
| 172 | | } |
|---|
| 173 | | |
|---|
| 174 | | connection = start_xmpp_client(username, |
|---|
| 175 | | password, |
|---|
| 176 | | xmpp_server, |
|---|
| 177 | | xmpp_server_port, |
|---|
| 178 | | xmpp_ssl_fingerprint, |
|---|
| 179 | | pubsub_server, |
|---|
| 180 | | NULL); |
|---|
| 181 | | |
|---|
| 182 | | if(connection == NULL) { |
|---|
| 183 | | g_printerr("Could not start client.\n"); |
|---|
| 184 | | return -1; |
|---|
| 185 | | } |
|---|
| 186 | | fclose(fp); |
|---|
| 187 | | if(debug_txt_flag) printf("Initialized XMPP client\n"); |
|---|
| 188 | | } |
|---|
| 189 | | |
|---|
| 190 | | |
|---|
| 191 | | fp=fopen( sampl_file_name,"r" ); |
|---|
| 192 | | if(fp==NULL) { |
|---|
| 193 | | printf( "SAMPL config: No %s file!\n",sampl_file_name ); |
|---|
| 194 | | printf( "This is required for sending control commands\n" ); |
|---|
| 195 | | exit(0); |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | // clear list of nodes |
|---|
| 199 | | node_list_init(); |
|---|
| 200 | | seq_num_cache_init(); |
|---|
| 201 | | |
|---|
| 202 | | sscanf(username,"%[^@]",name); |
|---|
| 203 | | node_list_add(name); |
|---|
| 204 | | |
|---|
| 205 | | if(xmpp_flag==1) |
|---|
| 206 | | { |
|---|
| 207 | | // generate parent node for gateway |
|---|
| 208 | | ret = create_event_node(connection, name,NULL,FALSE); |
|---|
| 209 | | if(ret != XMPP_NO_ERROR) { |
|---|
| 210 | | if(ret == XMPP_ERROR_NODE_EXISTS) |
|---|
| 211 | | if(debug_txt_flag) printf("Node '%s' already exists\n",name); |
|---|
| 212 | | else { |
|---|
| 213 | | g_printerr("Could not create event node '%s'. Error='%s'\n",name,ERROR_MESSAGE(ret)); |
|---|
| 214 | | return -1; |
|---|
| 215 | | } |
|---|
| 216 | | } else if(debug_txt_flag) printf("Created event node '%s'\n",name); |
|---|
| 217 | | |
|---|
| 218 | | } |
|---|
| 219 | | |
|---|
| 220 | | |
|---|
| 221 | | |
|---|
| 222 | | v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING); |
|---|
| | 81 | |
|---|
| | 82 | |
|---|
| | 83 | |
|---|
| | 84 | v=slipstream_open(slip_server,slip_port,NONBLOCKING); |
|---|
| | 85 | //v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING); |
|---|
| | 182 | |
|---|
| | 183 | |
|---|
| | 184 | |
|---|
| | 185 | |
|---|
| | 186 | |
|---|
| | 187 | } |
|---|
| | 188 | |
|---|
| | 189 | |
|---|
| | 190 | int main (int argc, char *argv[]) |
|---|
| | 191 | { |
|---|
| | 192 | GThread *main_thread = NULL; |
|---|
| | 193 | GError *error = NULL; |
|---|
| | 194 | GMainLoop *main_loop = NULL; |
|---|
| | 195 | |
|---|
| | 196 | char name[64]; |
|---|
| | 197 | char username[64]; |
|---|
| | 198 | char xmpp_file_name[128]; |
|---|
| | 199 | char sampl_file_name[128]; |
|---|
| | 200 | char password[64]; |
|---|
| | 201 | char xmpp_server[64]; |
|---|
| | 202 | char xmpp_ssl_fingerprint[64]; |
|---|
| | 203 | char pubsub_server[64]; |
|---|
| | 204 | uint32_t xmpp_server_port; |
|---|
| | 205 | uint8_t param; |
|---|
| | 206 | int32_t v,ret; |
|---|
| | 207 | |
|---|
| | 208 | debug_txt_flag=0; |
|---|
| | 209 | xmpp_flag=0; |
|---|
| | 210 | no_slip_flag=0; |
|---|
| | 211 | print_input_flag=0; |
|---|
| | 212 | connection=NULL; |
|---|
| | 213 | |
|---|
| | 214 | if (argc < 3 || argc > 6) { |
|---|
| | 215 | printf ("Usage: server port [-vxnf] [xmmp_config_name] [sampl_config_name]\n"); |
|---|
| | 216 | printf (" v Print Verbose Debug\n"); |
|---|
| | 217 | printf (" x Send data to XMPP server\n"); |
|---|
| | 218 | printf (" n Don't send SLIP packets (but receive them)\n"); |
|---|
| | 219 | printf (" f Use the following config files instead of the defaults\n"); |
|---|
| | 220 | exit (1); |
|---|
| | 221 | } |
|---|
| | 222 | |
|---|
| | 223 | strcpy( xmpp_file_name, "xmpp_config.txt" ); |
|---|
| | 224 | strcpy( sampl_file_name, "ff_config.txt" ); |
|---|
| | 225 | if(argc>3) |
|---|
| | 226 | { |
|---|
| | 227 | // Grab dash command line options |
|---|
| | 228 | if(strstr(argv[3],"v")!=NULL ) |
|---|
| | 229 | { |
|---|
| | 230 | printf( "Verbose Mode ON\n" ); |
|---|
| | 231 | debug_txt_flag=1; |
|---|
| | 232 | } |
|---|
| | 233 | if(strstr(argv[3],"x")!=NULL ) |
|---|
| | 234 | { |
|---|
| | 235 | printf( "XMPP ON\n" ); |
|---|
| | 236 | xmpp_flag=1; |
|---|
| | 237 | } |
|---|
| | 238 | if(strstr(argv[3],"n")!=NULL ) |
|---|
| | 239 | { |
|---|
| | 240 | printf( "SLIP TX OFF\n" ); |
|---|
| | 241 | no_slip_flag=1; |
|---|
| | 242 | } |
|---|
| | 243 | if(strstr(argv[3],"f")!=NULL ) |
|---|
| | 244 | { |
|---|
| | 245 | printf( "Loading XMPP config: " ); |
|---|
| | 246 | strcpy(xmpp_file_name, argv[4]); |
|---|
| | 247 | printf( "%s\n",xmpp_file_name ); |
|---|
| | 248 | printf( "Loading SAMPL config: " ); |
|---|
| | 249 | strcpy(sampl_file_name, argv[5]); |
|---|
| | 250 | printf( "%s\n",sampl_file_name ); |
|---|
| | 251 | } |
|---|
| | 252 | } |
|---|
| | 253 | |
|---|
| | 254 | if(xmpp_flag) |
|---|
| | 255 | { |
|---|
| | 256 | fp=fopen( xmpp_file_name,"r" ); |
|---|
| | 257 | if(fp==NULL) |
|---|
| | 258 | { |
|---|
| | 259 | printf( "XMPP config: No %s file!\n",xmpp_file_name ); |
|---|
| | 260 | exit(0); |
|---|
| | 261 | } |
|---|
| | 262 | param=0; |
|---|
| | 263 | do { |
|---|
| | 264 | v=fscanf( fp, "%[^\n]\n", buf); |
|---|
| | 265 | if(buf[0]!='#' && v!=-1) |
|---|
| | 266 | { |
|---|
| | 267 | switch(param) |
|---|
| | 268 | { |
|---|
| | 269 | case 0: strcpy(username,buf); break; |
|---|
| | 270 | case 1: strcpy(password,buf); break; |
|---|
| | 271 | case 2: strcpy(xmpp_server,buf); break; |
|---|
| | 272 | case 3: xmpp_server_port=atoi(buf); break; |
|---|
| | 273 | case 4: strcpy(pubsub_server,buf); break; |
|---|
| | 274 | case 5: strcpy(xmpp_ssl_fingerprint,buf); break; |
|---|
| | 275 | } |
|---|
| | 276 | param++; |
|---|
| | 277 | } |
|---|
| | 278 | |
|---|
| | 279 | }while(v!=-1 && param<6 ); |
|---|
| | 280 | |
|---|
| | 281 | if(debug_txt_flag) |
|---|
| | 282 | { |
|---|
| | 283 | printf( "XMPP Client Configuration:\n" ); |
|---|
| | 284 | printf( " username: %s\n",username ); |
|---|
| | 285 | printf( " password: %s\n",password); |
|---|
| | 286 | printf( " xmpp server: %s\n",xmpp_server); |
|---|
| | 287 | printf( " xmpp server port: %d\n",xmpp_server_port); |
|---|
| | 288 | printf( " xmpp pubsub server: %s\n",pubsub_server); |
|---|
| | 289 | printf( " xmpp ssl fingerprint: %s\n\n",xmpp_ssl_fingerprint); |
|---|
| | 290 | } |
|---|
| | 291 | if(param<4) |
|---|
| | 292 | { |
|---|
| | 293 | printf( "Not enough xmpp configuration parameters in xmpp_config.txt\n" ); |
|---|
| | 294 | exit(0); |
|---|
| | 295 | } |
|---|
| | 296 | |
|---|
| | 297 | connection = start_xmpp_client(username, |
|---|
| | 298 | password, |
|---|
| | 299 | xmpp_server, |
|---|
| | 300 | xmpp_server_port, |
|---|
| | 301 | xmpp_ssl_fingerprint, |
|---|
| | 302 | pubsub_server, |
|---|
| | 303 | NULL); |
|---|
| | 304 | |
|---|
| | 305 | if(connection == NULL) { |
|---|
| | 306 | g_printerr("Could not start client.\n"); |
|---|
| | 307 | return -1; |
|---|
| | 308 | } |
|---|
| | 309 | fclose(fp); |
|---|
| | 310 | if(debug_txt_flag) printf("Initialized XMPP client\n"); |
|---|
| | 311 | } |
|---|
| | 312 | |
|---|
| | 313 | |
|---|
| | 314 | fp=fopen( sampl_file_name,"r" ); |
|---|
| | 315 | if(fp==NULL) { |
|---|
| | 316 | printf( "SAMPL config: No %s file!\n",sampl_file_name ); |
|---|
| | 317 | printf( "This is required for sending control commands\n" ); |
|---|
| | 318 | exit(0); |
|---|
| | 319 | } |
|---|
| | 320 | |
|---|
| | 321 | // clear list of nodes |
|---|
| | 322 | node_list_init(); |
|---|
| | 323 | seq_num_cache_init(); |
|---|
| | 324 | |
|---|
| | 325 | cal_load_params("sensor_cal.txt"); |
|---|
| | 326 | |
|---|
| | 327 | g_thread_init (NULL); |
|---|
| | 328 | |
|---|
| | 329 | |
|---|
| | 330 | main_loop = g_main_loop_new(NULL,FALSE); |
|---|
| | 331 | |
|---|
| | 332 | main_thread = |
|---|
| | 333 | g_thread_create ((GThreadFunc) main_publish_loop, connection, TRUE, &error); |
|---|
| | 334 | if (error != NULL) { |
|---|
| | 335 | g_printerr ("Thread creation error: <%s>\n", error->message); |
|---|
| | 336 | return -1; |
|---|
| | 337 | } |
|---|
| | 338 | |
|---|
| | 339 | strcpy(slip_server,argv[1]); |
|---|
| | 340 | slip_port=atoi(argv[2]); |
|---|
| | 341 | if(xmpp_flag==1) |
|---|
| | 342 | { |
|---|
| | 343 | sscanf(username,"%[^@]",name); |
|---|
| | 344 | node_list_add(name); |
|---|
| | 345 | } |
|---|
| | 346 | |
|---|
| | 347 | if(xmpp_flag==1) |
|---|
| | 348 | { |
|---|
| | 349 | // generate parent node for gateway |
|---|
| | 350 | ret = create_event_node(connection, name,NULL,FALSE); |
|---|
| | 351 | if(ret != XMPP_NO_ERROR) { |
|---|
| | 352 | if(ret == XMPP_ERROR_NODE_EXISTS) |
|---|
| | 353 | if(debug_txt_flag) printf("Node '%s' already exists\n",name); |
|---|
| | 354 | else { |
|---|
| | 355 | g_printerr("Could not create event node '%s'. Error='%s'\n",name,ERROR_MESSAGE(ret)); |
|---|
| | 356 | return -1; |
|---|
| | 357 | } |
|---|
| | 358 | } else if(debug_txt_flag) printf("Created event node '%s'\n",name); |
|---|
| | 359 | |
|---|
| | 360 | } |
|---|
| | 361 | |
|---|
| | 362 | |
|---|
| | 363 | g_print("created thread\n"); |
|---|
| | 364 | g_main_loop_run (main_loop); |
|---|
| | 365 | |
|---|
| | 366 | |
|---|
| | 367 | |
|---|