static void wmmp_sendCmd(char *cmdstr) { char prefix_AT[] = "AT"; char enter[]="\r"; modem_write(prefix_AT,2); modem_write(cmdstr,strlen(cmdstr)); modem_write(enter,1); }
/**! @parem s_APNtag : 0002 @parem s_APNvalue : APN接入点 @ 此函数应该没用,APN模式下的接入点设置是修改的标准拨号脚本。zl */ int wmmp_setAPN(char *s_APNtag, const char *s_APNvalue) { #if (_CONFIG_M2M_MODULE != 0) char buf[256]; char ctrlz[1]={0x1A}; int rc; //确认已登录M2M平台 DEBUG_PRINTF("Send cmd...\n"); modem_sendCmd("AT$M2MALMRST=1234"); CommonSleep(20);//20ms printf("wmmp_setAPN\n"); //Enable parsing of TLV data. modem_sendCmd("AT$M2MBHV=2,1"); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAPN 1 WMMP get response error: %x\n",rc); DEBUG_PRINTF("wmmp_setAPN 1, rc = %d\n", rc); goto ExitProcessing; } //write TLV modem_sendCmd("AT$M2MCFG=1"); sleep(1); rc = modem_waitFor(">",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAPN 2 WMMP get response error: %x\n",rc); DEBUG_PRINTF("wmmp_setAPN 2, rc = %d\n", rc); goto ExitProcessing; } //write tag value sprintf(buf,"%s,%s",s_APNtag,s_APNvalue); DEBUG_PRINTF("Set Value: %s\n",buf); modem_write( buf, strlen(buf) ); modem_write( ctrlz, 1 ); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAppServer-3 WMMP get response error: %x\n",rc); DEBUG_PRINTF("wmmp_setAppServer 3, rc = %d\n", rc); goto ExitProcessing; } // printf("wmmp_setAppServer SUCCESS\n", rc); ExitProcessing: return rc; #else return 0; #endif }
static int receive_data() { // Set packet Header info *(uint64_t*)(&tx_buffer[0]) = MTU + PADDING_SIZE + HEADER_FRAME_SIZE; // Fill data into packet int k=0; for (k=0; k<TX_BUF_SIZE - HEADER_FRAME_SIZE; k=k+1) *(uint64_t*)(&tx_buffer[k+HEADER_DATA_SIZE]) = k; // Debug #if(DEBUG>0) printf("Header Data:\n"); for(int i = 0; i < HEADER_DATA_SIZE + HEADER_FRAME_SIZE; i++) printf("%d, ", tx_buffer[i]); printf("\n"); printf("Payload Data:\n"); for(int i = HEADER_DATA_SIZE + HEADER_FRAME_SIZE; i < TX_BUF_SIZE; i++) printf("%d, ", tx_buffer[i]); printf("\n"); #endif // Send to IP modem_write((uint64_t*)tx_buffer, TX_BUF_SIZE, 0); return 0; }
static void usb_modem_data_stress_test(struct modem *p, uint32_t duration) { struct timeval sub_tv; struct timeval ref_tv; struct timeval res_tv; time_t last_sec; uint8_t in_pending = 0; uint8_t in_ready = 0; uint8_t out_pending = 0; uint32_t id = 0; uint32_t in_max; uint32_t out_max; uint32_t io_max; uint8_t *in_buffer = 0; uint8_t *out_buffer = 0; gettimeofday(&ref_tv, 0); last_sec = ref_tv.tv_sec; printf("\n"); in_max = libusb20_tr_get_max_total_length(p->xfer_in); out_max = libusb20_tr_get_max_total_length(p->xfer_out); /* get the smallest buffer size and use that */ io_max = (in_max < out_max) ? in_max : out_max; if (in_max != out_max) printf("WARNING: Buffer sizes are un-equal: %u vs %u\n", in_max, out_max); in_buffer = malloc(io_max); if (in_buffer == NULL) goto fail; out_buffer = malloc(io_max); if (out_buffer == NULL) goto fail; while (1) { gettimeofday(&sub_tv, 0); if (last_sec != sub_tv.tv_sec) { printf("STATUS: ID=%u, RX=%u bytes/sec, TX=%u bytes/sec, ERR=%d\n", (int)id, (int)p->rx_bytes.bytes, (int)p->tx_bytes.bytes, (int)p->errors); p->rx_bytes.bytes = 0; p->tx_bytes.bytes = 0; fflush(stdout); last_sec = sub_tv.tv_sec; id++; } timersub(&sub_tv, &ref_tv, &res_tv); if ((res_tv.tv_sec < 0) || (res_tv.tv_sec >= (int)duration)) break; libusb20_dev_process(p->usb_dev); if (!libusb20_tr_pending(p->xfer_in)) { if (in_pending) { if (libusb20_tr_get_status(p->xfer_in) == 0) { modem_read(in_buffer, libusb20_tr_get_length(p->xfer_in, 0)); } else { p->errors++; usleep(10000); } in_pending = 0; in_ready = 1; } if (p->loop_data == 0) { libusb20_tr_setup_bulk(p->xfer_in, in_buffer, io_max, 0); libusb20_tr_start(p->xfer_in); in_pending = 1; in_ready = 0; } } if (!libusb20_tr_pending(p->xfer_out)) { uint32_t len; uint32_t dly; if (out_pending) { if (libusb20_tr_get_status(p->xfer_out) != 0) { p->errors++; usleep(10000); } } if (p->random_tx_length) { len = ((uint32_t)usb_ts_rand_noise()) % ((uint32_t)io_max); } else { len = io_max; } if (p->random_tx_delay) { dly = ((uint32_t)usb_ts_rand_noise()) % 16000U; } else { dly = 0; } if (p->loop_data != 0) { if (in_ready != 0) { len = libusb20_tr_get_length(p->xfer_in, 0); memcpy(out_buffer, in_buffer, len); in_ready = 0; } else { len = io_max + 1; } if (!libusb20_tr_pending(p->xfer_in)) { libusb20_tr_setup_bulk(p->xfer_in, in_buffer, io_max, 0); libusb20_tr_start(p->xfer_in); in_pending = 1; } } else { modem_write(out_buffer, len); } if (len <= io_max) { libusb20_tr_setup_bulk(p->xfer_out, out_buffer, len, 0); if (dly != 0) usleep(dly); libusb20_tr_start(p->xfer_out); out_pending = 1; } } libusb20_dev_wait_process(p->usb_dev, 500); if (libusb20_dev_check_connected(p->usb_dev) != 0) { printf("Device disconnected\n"); break; } } libusb20_tr_stop(p->xfer_in); libusb20_tr_stop(p->xfer_out); printf("\nData stress test done!\n"); fail: if (in_buffer) free(in_buffer); if (out_buffer) free(out_buffer); }
int wmmp_setAppServer(int flowID, const char *server_ip, const char *port) { char buf[256]; char ctrlz[1]={0x1A}; int rc; // 锁定中国移动网络,防止信号不稳定或与其他运营商有干扰时, //中国移动网络的ID号被添加在SIM卡的FPLMN(禁止访问的网络)文件中,造成自动选择网络时被屏蔽。 //该命令与设置M2M模块IP无关,放这只是想省事,款机使用过程中只执行一次。 DEBUG_PRINTF("Send cmd...\n"); #if (SIMCOM == 1) modem_sendCmd("at+cops=1,2,\"46000\""); //by yy 20110819 #else modem_sendCmd("at+cops=1,2,46000;&w"); #endif DEBUG_PRINTF("Wait response...\n"); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("锁定中国移动网络 WMMP get response error: %x\n",rc); //printf("wmmp_setAppServer, rc = %d\n", rc); goto ExitProcessing; } #if (SIMCOM == 1) //add by yy 20110819 DEBUG_PRINTF("Send cmd at&w\n"); // printf("Send cmd at&w\n"); modem_sendCmd("at&w"); DEBUG_PRINTF("Wait response...\n"); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("锁定中国移动网络 WMMP get response error: %x\n",rc); //printf("wmmp_setAppServer 1, rc = %d\n", rc); goto ExitProcessing; } #endif //Enable parsing of TLV data. DEBUG_PRINTF("Send cmd...\n"); modem_sendCmd("AT$M2MBHV=2,1"); DEBUG_PRINTF("Wait response...\n"); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAppServer-1 WMMP get response error: %x\n",rc); // printf("wmmp_setAppServer 1, rc = %d\n", rc); goto ExitProcessing; } //确认已登录M2M平台 DEBUG_PRINTF("Send cmd...\n"); modem_sendCmd("AT$M2MALMRST=1234"); CommonSleep(20);//20ms // printf("wmmp_setAppServer 1.1\n"); //write TLV DEBUG_PRINTF("Send cmd...\n"); modem_sendCmd("AT$M2MCFG=1"); DEBUG_PRINTF("Wait response...\n"); rc = modem_waitFor(">",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAppServer-2 WMMP get response error: %x\n",rc); // printf("wmmp_setAppServer 2, rc = %d\n", rc); goto ExitProcessing; } //write tag value sprintf(buf,"D00%X,4,\"tcp://%s:%s\"",(flowID+1),server_ip,port); DEBUG_PRINTF("Set Value: %s\n",buf); modem_write( buf, strlen(buf) ); modem_write( ctrlz, 1 ); rc = modem_waitFor("OK",WMMP_TIMEOUT); if(rc != 0) { DEBUG_PRINTF("wmmp_setAppServer-3 WMMP get response error: %x\n",rc); // printf("wmmp_setAppServer 3, rc = %d\n", rc); goto ExitProcessing; } // printf("wmmp_setAppServer SUCCESS\n", rc); ExitProcessing: return rc; }
static int wmmp_trap(unsigned short tag,char *value) { char buf[256]; char ctrlz[1]={0x1A}; int transID1,transID2; int rc; //flush input buffer modem_flushInputBuffer(); //Enable parsing of TLV data. DEBUG_PRINTF("Send cmd...\n"); modem_sendCmd("AT$M2MTRAP=1"); DEBUG_PRINTF(("WMMP wait for prompt \n")); rc = wmmp_waitResponse(NULL,20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } if(wmmp_rx_buf.enumResponseType != RESPONSE_PROMPT) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } //write tag value sprintf(buf,"%X,%s",(tag&0xffff),value); DEBUG_PRINTF("trap TLV : %s\n",buf); modem_write( buf, strlen(buf) ); modem_write( ctrlz, 1 ); //wait response DEBUG_PRINTF("WMMP wait response \n"); rc = wmmp_waitResponse("$M2MTRAP",20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_CMD_OK) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } if(sscanf(wmmp_rx_buf.line[0],"$M2MTRAP: %d",&transID1) != 1) { DEBUG_PRINTF("transID not found\n"); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("transID1 = %d\n",transID1); //wait async acks DEBUG_PRINTF("WMMP wait response \n"); rc = wmmp_waitResponse(NULL,20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_ACK) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } if(sscanf(wmmp_rx_buf.line[0],"$M2MACK: %d",&transID2) != 1) { DEBUG_PRINTF("transID not found\n"); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("transID2 = %d\n",transID2); if(transID1 != transID2) { DEBUG_PRINTF("transID1 != transID2. transID1=%d, transID2=%d\n",transID1,transID2); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } ExitProcessing: return rc; }
int wmmp_sendAppData(int flowID, char *buf, int len) { char cmdbuf[128]; char ctrlz[1]={0x1A}; int tmplen; int transID1,transID2; int rc; int wlen; int readlen; char sendbuf[APP_SEND_PACKET_MAXLEN*2+1]=""; char *rp; rp = buf; int i = 0; do { tmplen = len > APP_SEND_PACKET_MAXLEN ? APP_SEND_PACKET_MAXLEN : len; // check status // DEBUG_PRINTF(("WMMP get status \n")); // rc = wmmp_checkStatus(); // if( rc != 0 ) // { // DEBUG_PRINTF(("WMMP ready for operation \n")); // goto ExitProcessing; // } //flush input buffer modem_flushInputBuffer(); //send cmd DEBUG_PRINTF(("WMMP send cmd \n")); sprintf( cmdbuf, "AT$M2MAPPSEND=%d", flowID); modem_sendCmd( cmdbuf ); // printf("**i = %d tmplen = %d", i++, tmplen); // printf("**send cmdbuf = %s", cmdbuf); DEBUG_PRINTF(("WMMP wait for prompt \n")); // rc = modem_waitFor(">",10); // if(rc != 0) // { // DEBUG_PRINTF(("WMMP get prompt error: %x\n",rc)); // goto ExitProcessing; // } rc = wmmp_waitResponse(NULL,20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } if(wmmp_rx_buf.enumResponseType != RESPONSE_PROMPT) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } #if 0 //RAW data mode //write data DEBUG_PRINTF("WMMP send data,len =%d\n",tmplen); modem_write( rp, tmplen ); //terminate modem_write( ctrlz, 1 ); #else //HEX encoded data mode //HEX encode the output data wlen = hex2str(rp,tmplen,sendbuf,(APP_SEND_PACKET_MAXLEN*2+1)); //write data DEBUG_PRINTF("WMMP send data,len =%d\n",wlen); modem_write( sendbuf, wlen ); //terminate modem_write( ctrlz, 1 ); #endif //wait response DEBUG_PRINTF("WMMP wait response \n"); rc = wmmp_waitResponse("$M2MAPPSEND",20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } // printf("rev1 = %s\n", wmmp_rx_buf.line[0]); if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_CMD_OK) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } if(sscanf(wmmp_rx_buf.line[0],"$M2MAPPSEND: %d",&transID1) != 1) { DEBUG_PRINTF("transID not found\n"); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("transID1 = %d\n",transID1); #if 1 //wait async acks DEBUG_PRINTF("WMMP wait response \n"); rc = wmmp_waitResponse(NULL,20); if (rc != 0) { DEBUG_PRINTF("error: rc = %x\n",rc); goto ExitProcessing; } // printf("rev2 = %s\n", wmmp_rx_buf.line[0]); if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_ACK) { DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } if(sscanf(wmmp_rx_buf.line[0],"$M2MACK: %d",&transID2) != 1) { DEBUG_PRINTF("transID not found\n"); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("transID2 = %d\n",transID2); if(transID1 != transID2) { DEBUG_PRINTF("transID1 != transID2. transID1=%d, transID2=%d\n",transID1,transID2); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } #endif /* #if 1 //add by yy 20110818 //wait async $MSG DEBUG_PRINTF("WMMP wait response \n"); printf("**WMMP wait response $MSG \n"); //for debug rc = wmmp_waitResponse(NULL,20); if (rc != 0) { DEBUG_PRINTF("**error: rc = %x\n",rc); goto ExitProcessing; } printf("rev3 = %s", wmmp_rx_buf.line[0]); if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_SEND_MSG) { DEBUG_PRINTF("**unexpected response: %d\n",wmmp_rx_buf.enumResponseType); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("here\n"); // if(sscanf(wmmp_rx_buf.line[0],"$M2M$MSG: %d, %",&transID2) != 1) if(sscanf(wmmp_rx_buf.line[0],"$M2M$MSG: 7,%d,%d",&transID2,&readlen) != 2) { DEBUG_PRINTF("**transID not found\n"); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("transID2 = %d\n",transID2); printf("transID2 = %d\n",transID2); //for debug if(transID1 != transID2) { DEBUG_PRINTF("**transID2 = %d\n",transID2); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } DEBUG_PRINTF("readlen = %d\n",readlen); printf("readlen = %d\n",readlen); //for debug //比长度 if(readlen != tmplen) { DEBUG_PRINTF("**readlen = %d, tmplen = %d\n",readlen, tmplen); rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE); goto ExitProcessing; } #endif*/ //update len and buf pointer len -= tmplen; rp = buf +tmplen; }while( len > 0 ); ExitProcessing: return rc; }
void modem_writeline(const char *buffer) { modem_write((const uint8_t *) buffer, strlen(buffer)); modem_write((const uint8_t *) "\r\n", 2); }