/** * Main system entry point */ int main (void) { SystemInit(); /* Initialise Pins */ CUTDOWN_OFF(); HEATER_OFF(); MBED_OFF(); GREEN_OFF(); /* Update the value of SystemCoreClock */ SystemCoreClockUpdate(); /* Initialise Interfaces */ i2c_init(); spi_init(process_imu_frame); // IMU sd_spi_init(); // SD uart_init(); // GPS pwrmon_init(); // ADC /* Initialise Sensors */ init_barometer(); /* SD Card */ if (initialise_card()) { // Initialised to something if (disk_initialize() == 0) { // Disk initialisation was successful sd_good = 1; } } GREEN_ON(); /* Configure the SysTick */ NVIC_SetPriority(SysTick_IRQn, 0); // Highest Priority Interrupt SysTick_Config(SystemCoreClock / RTTY_BAUD); /* Watchdog - Disabled for debugging */ #ifndef WATCHDOG_DISABLED init_watchdog(); #endif struct barometer* b; struct imu_raw ir; struct gps_data gd; struct gps_time gt; double alt, ext_temp; int tx_length; // The length of the built tx string char tx_string[TX_STRING_LENGTH]; while (1) { /* Grab Data */ pwrmon_start(pwrmon_callback); b = get_barometer(); get_imu_raw_data(&ir); get_gps_data(&gd); get_gps_time(>); ext_temp = get_temperature(); /* Data Processing */ if (b->valid) { alt = pressure_to_altitude(b->pressure); } else { alt = -1; b->temperature = -1; } /* Act on the data */ control_gsm(alt); control_cutdown(ticks_until_cutdown, alt); control_heater(b->temperature); /* Create a protocol string */ int cutstat; if (ticks_until_cutdown == 0) { cutstat = -1; } else { cutstat = ticks_until_cutdown / (RTTY_BAUD*60); } tx_length = build_communications_frame(tx_string, TX_STRING_LENGTH, >, b, &gd, alt, ext_temp, &ir, cutstat, cutdown_voltage); /* Transmit - Quietly fails if another transmission is ongoing */ rtty_set_string(tx_string, tx_length); /* Store */ if (sd_good) { tx_length -= 2; // Remove \n\0 tx_length += communications_frame_add_extra(tx_string + tx_length, TX_STRING_LENGTH - tx_length, &ir); disk_write_next_block((uint8_t*)tx_string, tx_length+1); // Include null terminator } /* Housekeeping */ GREEN_TOGGLE(); feed_watchdog(); } }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // Event Function //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ void gps_gateway_process(int x, short int y, void *pargs) { unsigned int unFromAddrLen; int nByte = 0; int nSendByte = 0; unsigned char aReqBuffer[512]; unsigned char aRespBuffer[512]; struct sockaddr_in stFromAddr; bson_oid_t oid; bson_t *doc; const bson_t *result; bson_t *query; int i = 0; int command = 0; int monitor_type = 0; char telephone[10]; char text_message[240]; int length; char strBaseinfo[10]; AGPS_info * agps; cell_info * cell; unFromAddrLen = sizeof(stFromAddr); memset(aReqBuffer,0x00,512); memset(aRespBuffer,0x00,512); if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0, (struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1) { printf("error occured while receivingn"); } //proc(); data_frame_head *msg = (data_frame_head *) aReqBuffer; content_gps_up *g = (content_gps_up *)(aReqBuffer + sizeof(data_frame_head)); //异或校验 //异或校验 unsigned char xor = *(aReqBuffer + nByte - 2); if( xor != check_xor(aReqBuffer, nByte - 2)) { return ;//drop down BAD check xor. } switch (msg->cmd) { case CMD_TERMINAL_GPS_UP : //解析位置信息,得到经纬度 fprintf(stderr,"protocal head %x \n",msg->data_head[0] ); fprintf(stderr,"cmd %x \n", msg->cmd ); fprintf(stderr,"length %d \n", ntohs(msg->length) ); //fprintf(stderr,"product_id %u\n",get_product_id(msg->terminal_id)); fprintf(stderr,"product_id %u\n",get_product_id(g->terminal_id)); fprintf(stderr,"flow_id %x \n", ntohs(g->flow_id) ); fprintf(stderr,"data_type %x \n", g->data_type ); fprintf(stderr," time " ); for ( i = 0 ; i< 6 ;i++) fprintf(stderr," %x \n", g->base_info.date_time[i] ); ///////// //经纬度 fprintf(stderr," latitude " ); for ( i = 0 ; i< 4 ;i++) fprintf(stderr,"%x \n", g->base_info.latitude[i] ); fprintf(stderr," longitude " ); for ( i = 0 ; i< 4 ;i++) fprintf(stderr," %x \n", g->base_info.longitude[i] ); ///////// fprintf(stderr,"speed %u \n", ntohs(g->base_info.speed)); fprintf(stderr,"fangxiang %u \n", ntohs(g->base_info.direction)); fprintf(stderr,"gaodu %u\n", ntohs(g->base_info.high)); fprintf(stderr,"定位状态 %c\n", g->base_info.pos_station); fprintf(stderr,"是否应答 %x\n", g->base_info.need_response); if (ntohs(msg->length) > sizeof (content_gps_up)) { fprintf(stderr,"含有附带信息\n"); unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up); fprintf(stderr,"附带信息类型:%x\n",*attach_info); fprintf(stderr,"附带信息长度:%d\n",*(attach_info+1)); switch (*(attach_info)) { case 0x01: break; case 0x02: fprintf(stderr,"基站信息\n"); agps = (AGPS_info * ) ( attach_info + 2); cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info)); fprintf(stderr,"\n国家编码"); fprintf(stderr," %x ", ntohl(agps->country_code) ); fprintf(stderr,"\n运营编码"); fprintf(stderr," %x ", ntohl(agps->mobile_code) ); fprintf(stderr,"\n运基站定位\n"); for ( i = 0 ; i< agps->base_count ;i++){ fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) ); fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) ); fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN ); cell++; } break; case 0x0C: case 0x1E: case 0x1F: case 0x28: case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: break; } } if( g->data_type == 0x80 || g->data_type == 0x82 || g->data_type == 0x8E ) { //保存mongodb unsigned char timegps[15]; unsigned char latitude[10]; unsigned char longitude[10]; double lat; double lng; doc = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (doc, "_id", &oid); BSON_APPEND_INT32 (doc, "id",get_product_id(g->terminal_id)); get_gps_time(g->base_info.date_time, timegps ); BSON_APPEND_UTF8 (doc, "timestamp", timegps); lat = bcd2longitude(g->base_info.latitude, latitude ); lng = bcd2longitude(g->base_info.longitude, longitude ); BSON_APPEND_UTF8 (doc, "latitude", latitude); BSON_APPEND_UTF8 (doc, "longitude", longitude); BSON_APPEND_DOUBLE (doc, "lat", lat); BSON_APPEND_DOUBLE (doc, "lng", lng); BSON_APPEND_INT32 (doc, "speed", ntohs(g->base_info.speed)); BSON_APPEND_INT32 (doc, "direction", ntohs(g->base_info.direction)); BSON_APPEND_INT32 (doc, "high", ntohs(g->base_info.high)); BSON_APPEND_INT32 (doc, "pos_station", g->base_info.pos_station); if (ntohs(msg->length) > sizeof (content_gps_up)) { unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up); switch (*(attach_info)) { case 0x01: break; case 0x02: agps = (AGPS_info * ) ( attach_info + 2); cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info)); fprintf(stderr,"\n国家编码"); fprintf(stderr," %x ", ntohl(agps->country_code) ); BSON_APPEND_INT32 (doc, "country_code", ntohl(agps->country_code)); fprintf(stderr,"\n运营编码"); fprintf(stderr," %x ", ntohl(agps->mobile_code) ); BSON_APPEND_INT32 (doc, "mobile_code", ntohl(agps->mobile_code)); fprintf(stderr,"\n运基站定位\n"); for ( i = 0 ; i< agps->base_count ;i++){ fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) ); fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) ); fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN ); sprintf(strBaseinfo,"lac%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->lacN) ); sprintf(strBaseinfo,"cell%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->cellN) ); sprintf(strBaseinfo,"signal%d",i); BSON_APPEND_INT32 (doc, strBaseinfo, cell->signalN ); cell++; } break; case 0x0C: case 0x1E: case 0x1F: case 0x28: case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: break; } } if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { printf ("Insert failed: %s\n", error.message); } bson_destroy (doc); } else if( g->data_type == 0x85 ) { //delete from mongo query = bson_new (); BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id)); if( ! mongoc_collection_remove(collection_cmd, MONGOC_DELETE_NONE, query, NULL, &error)) { fprintf(stderr,"remove command failed \n" ); } bson_destroy (query); } if (g->base_info.need_response == 0x01) { send_22_response( x, aRespBuffer , g->flow_id, xor , g->data_type, stFromAddr, unFromAddrLen ); } break; case CMD_TERMINAL_INFO_UP : fprintf(stderr,"CMD_TERMINAL_INFO_UP\n"); content_info_up * t = (content_info_up *)(aReqBuffer + sizeof(data_frame_head)); fprintf(stderr,"parameter %x \n", t->parameter ); if(t->parameter == 0x01) { sent_time_response( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } break; case CMD_VERSION_INFO_UP : fprintf(stderr,"CMD_VERSION_INFO_UP"); break; case CMD_VOICE_UP : fprintf(stderr,"CMD_VOICE_UP"); break; default: break; } //get command from mongo char *str; query = bson_new (); BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id)); cursor = mongoc_collection_find (collection_cmd, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); //while (mongoc_cursor_next (cursor, &doc)) { // mongoc_cursor_next (cursor, &result); while (mongoc_cursor_next (cursor, (const bson_t **) &doc)) { bson_iter_t iter; bson_iter_t sub_iter; str = bson_as_json (doc, NULL); fprintf (stderr, "%s\n", str); bson_free (str); if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "cmd", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); command = (int)bson_iter_int32 (&sub_iter); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "parameter", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); strcpy (telephone , bson_iter_utf8 (&sub_iter,&length)); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "monitor_type", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); monitor_type = (int)bson_iter_int32 (&sub_iter); } if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "text_message", &sub_iter)) { fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter)); printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter)); strcpy (text_message , bson_iter_utf8 (&sub_iter,&length)); } } mongoc_cursor_destroy (cursor); bson_destroy (query); //send command to handring if (command == 1) { send_monitor_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ,monitor_type, telephone); } else if (command == 9) { send_sleep_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } else if (command == 2) { send_set_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ,text_message); } else{ //为了避免长时间使用网络。在通信6次发送一个休眠指令 if( (msg->cmd == CMD_TERMINAL_GPS_UP) && (check_state(g->terminal_id) == MAX_STANDBY) ) { send_sleep_cmd( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen ); } } //printf("Function called buffer is %sn",aReqBuffer); g_count++; }