//========================== int https_post(lua_State* L) { int ret; vm_https_callbacks_t callbacks = { (vm_https_set_channel_response_callback)https_post_request_set_channel_rsp_cb, (vm_https_unset_channel_response_callback)https_unset_channel_rsp_cb, (vm_https_release_all_request_response_callback)https_send_release_all_req_rsp_cb, (vm_https_termination_callback)https_send_termination_ind_cb, (vm_https_send_response_callback)https_send_read_request_rsp_cb, (vm_https_read_content_response_callback)https_send_read_read_content_rsp_cb, (vm_https_cancel_response_callback)https_send_cancel_rsp_cb, (vm_https_status_query_response_callback)https_send_status_query_rsp_cb }; size_t sl; const char* url = luaL_checklstring(L, 1, &sl); free_post_buffers(); g_https_url = vm_calloc(sl+1); if (g_https_url == NULL) { return luaL_error(L, "url buffer allocation error"); } strncpy(g_https_url, url, sl); int totalalloc = (sl+1); if ((!lua_istable(L, 2)) && (!lua_isstring(L, 2))) { free_post_buffers(); return luaL_error(L, "POST data arg missing" ); } if (lua_isstring(L, 2)) { g_post_type = 0; const char* pdata = luaL_checklstring(L, 2, &sl); if ((sl <= 0) || (pdata == NULL)) { free_post_buffers(); return luaL_error(L, "wrong post data"); } g_postdata = vm_calloc(sl+1); if (g_postdata == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error"); } totalalloc += (sl+1); strncpy(g_postdata, pdata, sl); g_postdata[sl] = '\0'; g_postdata_len = sl; vm_log_debug("[POST]: allocated memory = %d", totalalloc); } else if (lua_istable(L, 2)) { g_post_type = 1; // ** iterate the table to find number of entries and total data size int pdata_size = 0; int nentries = 0; int fnames_size = 0; int filenames_size = 0; int pathnames_size = 0; int err = 0; int nfileentry = 0; lua_pushnil(L); // first key while (lua_next(L, 2) != 0) { // Pops a key from the stack, and pushes a key-value pair from the table // 'key' (at index -2) and 'value' (at index -1) if ((lua_isstring(L, -1)) && (lua_isstring(L, -2))) { size_t klen = 0; size_t vlen = 0; const char* key = lua_tolstring(L, -2, &klen); const char* value = lua_tolstring(L, -1, &vlen); if ((klen > 0) && (vlen > 0)) { nentries++; fnames_size += (klen+1); if (strstr(key, "file") == key) { if (nfileentry < 1) { nfileentry++; filenames_size += (vlen+1); char fn[128]; short wfn[128]; sprintf(fn, "C:\\%s", value); vm_chset_ascii_to_ucs2((VMWSTR)wfn, 256, fn); int fh = vm_fs_open(wfn, VM_FS_MODE_READ, VM_TRUE); if (fh < 0) { err++; vm_log_debug("[POST]: File '%s' not found",fn); } else vm_fs_close(fh); int fnmlen = (vm_wstr_string_length(wfn)+1)*2; pathnames_size += fnmlen; } else { vm_log_debug("[POST]: Only 1 file allowed!"); } } else pdata_size += (vlen+sizeof(int)); } } lua_pop(L, 1); // removes 'value'; keeps 'key' for next iteration } if (nentries == 0) { free_post_buffers(); return luaL_error(L, "no post entries"); } if (err != 0) { free_post_buffers(); return luaL_error(L, "file not found"); } // ** Allocate buffers g_post_context = vm_calloc(sizeof(vm_https_request_context_t)); if (g_post_context == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 1"); } totalalloc += sizeof(vm_https_request_context_t); g_post_content = vm_calloc(sizeof(vm_https_content_t)*nentries); if (g_post_content == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 2"); } totalalloc += (sizeof(vm_https_content_t)*nentries); if (pdata_size > 0) { g_postdata = vm_calloc(pdata_size); if (g_postdata == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 3"); } totalalloc += pdata_size; } g_https_field_names = vm_calloc(fnames_size); if (g_https_field_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 4"); } totalalloc += fnames_size; if (filenames_size > 0) { g_https_file_names = vm_calloc(filenames_size); if (g_https_file_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 5"); } totalalloc += filenames_size; } if (pathnames_size > 0) { g_https_path_names = vm_calloc(pathnames_size); if (g_https_path_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 6"); } totalalloc += pathnames_size; } vm_log_debug("[POST]: allocated memory = %d", totalalloc); sprintf(g_https_url, url); g_post_context->number_entries = 0; g_post_context->content = (vm_https_content_t *)g_post_content; g_post_context->header = CONTENT_TYPE_FORMDATA; g_post_context->header_length = strlen(CONTENT_TYPE_FORMDATA); g_post_context->post_segment = NULL; g_post_context->post_segment_length = 0; g_post_context->url = g_https_url; g_post_context->url_length = strlen(g_post_context->url); // ** iterate the table and populate data buffers vm_https_content_t cc; g_postdata_len = 0; fnames_size = 0; filenames_size = 0; pathnames_size = 0; nfileentry = 0; lua_pushnil(L); while (lua_next(L, 2) != 0) { if ((lua_isstring(L, -1)) && (lua_isstring(L, -2))) { size_t klen = 0; size_t vlen = 0; const char* key = lua_tolstring(L, -2, &klen); const char* value = lua_tolstring(L, -1, &vlen); if ((klen > 0) && (vlen > 0)) { // field key & value are OK g_post_context->number_entries++; if (strstr(key, "file") == key) { if (nfileentry < 1) { nfileentry++; cc.data_type = VM_HTTPS_DATA_TYPE_FILE; cc.content_type = CONTENT_TYPE_OCTET; cc.content_type_length = strlen(CONTENT_TYPE_OCTET); // save file name & length sprintf(g_https_file_names+filenames_size, "%s", value); cc.filename = g_https_file_names+filenames_size; cc.filename_length = vlen; filenames_size += (vlen+1); // save local file path/name & length char fn[128]; short wfn[128]; sprintf(fn, "C:\\%s", value); vm_chset_ascii_to_ucs2((VMWSTR)wfn, 256, fn); int wfnlen = vm_wstr_copy(g_https_path_names+pathnames_size, wfn); cc.file_path_name = g_https_path_names+pathnames_size; cc.file_path_name_length = vm_wstr_string_length(cc.file_path_name); pathnames_size += ((cc.file_path_name_length+1)*2); // get file size int fh = vm_fs_open(cc.file_path_name, VM_FS_MODE_READ, VM_TRUE); VMUINT fsz; if (vm_fs_get_size(fh, &fsz) == 0) cc.data_length = fsz; else cc.data_length = 0; vm_fs_close(fh); } } else { cc.data_type = VM_HTTPS_DATA_TYPE_BUFFER; cc.content_type = CONTENT_TYPE_TEXT; cc.content_type_length = strlen(CONTENT_TYPE_TEXT); cc.filename = ""; cc.filename_length = 0; cc.file_path_name = (VMWSTR)"\0\0"; cc.file_path_name_length = 0; cc.data_length = vlen; // field data length int dlen = vlen; memcpy(g_postdata+g_postdata_len, &dlen, sizeof(int)); memcpy(g_postdata+g_postdata_len+sizeof(int), value, vlen); g_postdata_len += (vlen+sizeof(int)); } cc.charset = VM_HTTPS_CHARSET_ASCII; // save field name & length sprintf(g_https_field_names+fnames_size, "%s", key); cc.name = g_https_field_names+fnames_size; cc.name_length = klen; // field name length fnames_size += (klen+1); memcpy(g_post_content + (sizeof(vm_https_content_t) * (g_post_context->number_entries-1)), &cc, sizeof(vm_https_content_t)); } } // removes 'value'; keeps 'key' for next iteration lua_pop(L, 1); } } ret = vm_https_register_context_and_callback(gprs_bearer_type, &callbacks); if (ret != 0) { free_post_buffers(); l_message(NULL, "register context & cb failed"); } else { ret = vm_https_set_channel(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } lua_pushinteger(L, ret); return 1; }
void sms_read_callback(vm_gsm_sms_callback_t* callback_data) { static char phone_number[42]; //static char content[301]; vm_gsm_sms_read_message_data_callback_t* read_msg; VMINT8 *g_sms_number_buf = 0; VMINT8 *g_sms_content_buf = 0; vm_log_info("got new message"); if(callback_data->action == VM_GSM_SMS_ACTION_READ) { if(callback_data->cause == VM_GSM_SMS_CAUSE_NO_ERROR) { if(callback_data->action_data) { read_msg = (vm_gsm_sms_read_message_data_callback_t*)callback_data->action_data; int size = vm_wstr_string_length((VMWCHAR*)read_msg->message_data->number); g_sms_number_buf = phone_number; vm_chset_ucs2_to_ascii((signed char *)g_sms_number_buf, 42, (VMWCHAR*)read_msg->message_data->number); // assume dcs = UCS2 g_sms_content_buf = content; vm_chset_ucs2_to_ascii((signed char *)g_sms_content_buf, MAX_CONTENT_BUFFER, (VMWCHAR*)read_msg->message_data->content_buffer); VMUINT16 i,j = 0; VMUINT16 str_len; VMUINT8 str_num; str_len = strlen(g_sms_content_buf); str_num = str_len / 151 + 1; vm_log_info("str_len is %d", str_len); vm_log_info("str_num is %d", str_num); while(1) { msgIdMax ++; if(msgIdMax >= GSM_SMS_NUM_MAX)msgIdMax = GSM_SMS_NUM_MAX; for(i=0;i<(GSM_SMS_NUM_MAX - 1);i++) { strcpy(msgNumber[(GSM_SMS_NUM_MAX - 2 - i) + 1], msgNumber[(GSM_SMS_NUM_MAX - 2) - i]); strcpy(msgContent[(GSM_SMS_NUM_MAX - 2 - i) + 1],msgContent[(GSM_SMS_NUM_MAX - 2) - i]); } strcpy(msgNumber[0], (char*)g_sms_number_buf); if(j < (str_len - 1)) strncpy(msgContent[0], (char*)(g_sms_content_buf + j * 150), 150); else strncpy(msgContent[0], (char*)(g_sms_content_buf + j * 150), str_len - j * 150); j ++; if(j == str_num)break; } vm_log_info("save message"); sms_inbox_save(); vm_log_info("saved"); if (g_sms_new_message_cb) { g_sms_new_message_cb(g_sms_number_buf, g_sms_content_buf); //g_sms_new_message_cb(msgNumber[0], msgContent[0]); } vm_log_info("callback done"); } } else { vm_log_info("read msg failed"); } } }