struct mg_str mg_url_encode_opt(const struct mg_str src, const struct mg_str safe, unsigned int flags) { const char *hex = (flags & MG_URL_ENCODE_F_UPPERCASE_HEX ? "0123456789ABCDEF" : "0123456789abcdef"); size_t i = 0; struct mbuf mb; mbuf_init(&mb, src.len); for (i = 0; i < src.len; i++) { const unsigned char c = *((const unsigned char *) src.p + i); if (isalnum(c) || mg_strchr(safe, c) != NULL) { mbuf_append(&mb, &c, 1); } else if (c == ' ' && (flags & MG_URL_ENCODE_F_SPACE_AS_PLUS)) { mbuf_append(&mb, "+", 1); } else { mbuf_append(&mb, "%", 1); mbuf_append(&mb, &hex[c >> 4], 1); mbuf_append(&mb, &hex[c & 15], 1); } } mbuf_append(&mb, "", 1); mbuf_trim(&mb); return mg_mk_str_n(mb.buf, mb.len - 1); }
void cc3200_console_cloud_push() { if (s_cctx.buf.len == 0 || !s_cctx.initialized) return; int l = cc3200_console_next_msg_len(); if (l == 0) return; // Only send full messages. struct clubby *c = sj_clubby_get_global(); if (c == NULL || !sj_clubby_is_connected(c)) { /* If connection drops, do not wait for reply as it may never arrive. */ s_cctx.request_in_flight = 0; return; } if (s_cctx.request_in_flight || !sj_clubby_can_send(c)) return; s_cctx.request_in_flight = 1; s_cctx.in_console = 1; sj_clubby_call(c, NULL, "/v1/Log.Log", mg_mk_str_n(s_cctx.buf.buf, l - 1), 0, clubby_cb, NULL); mbuf_remove(&s_cctx.buf, l); if (s_cctx.buf.len == 0) mbuf_trim(&s_cctx.buf); s_cctx.in_console = 0; }
static void mg_rpc_ws_handler(struct mg_connection *nc, int ev, void *ev_data) { struct mg_rpc_channel *ch = (struct mg_rpc_channel *) nc->user_data; struct mg_rpc_channel_ws_data *chd = (struct mg_rpc_channel_ws_data *) ch->channel_data; switch (ev) { case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { LOG(LL_INFO, ("%p WS HANDSHAKE DONE", ch)); ch->ev_handler(ch, MG_RPC_CHANNEL_OPEN, NULL); break; } case MG_EV_WEBSOCKET_FRAME: { struct websocket_message *wm = (struct websocket_message *) ev_data; struct mg_str f = mg_mk_str_n((const char *) wm->data, wm->size); ch->ev_handler(ch, MG_RPC_CHANNEL_FRAME_RECD, &f); break; } case MG_EV_SEND: { if (!chd->sending) break; int num_sent = *((int *) ev_data); if (num_sent < 0) { chd->sending = false; ch->ev_handler(ch, MG_RPC_CHANNEL_FRAME_SENT, (void *) 0); } else if (nc->send_mbuf.len == 0) { chd->sending = false; ch->ev_handler(ch, MG_RPC_CHANNEL_FRAME_SENT, (void *) 1); } break; } case MG_EV_CLOSE: { LOG(LL_INFO, ("%p CLOSED", ch)); if (chd->sending) { ch->ev_handler(ch, MG_RPC_CHANNEL_FRAME_SENT, (void *) 0); } ch->ev_handler(ch, MG_RPC_CHANNEL_CLOSED, NULL); if (chd->free_data) { free(chd); ch->channel_data = NULL; } break; } } }
static int tcmp(const struct json_token *tok, const char *str) { struct mg_str s = {.p = tok->ptr, .len = tok->len}; return mg_vcmp(&s, str); } enum mgos_upd_file_action mgos_upd_file_begin( struct mgos_upd_hal_ctx *ctx, const struct mgos_upd_file_info *fi) { struct mg_str part_name = MG_MK_STR(""); enum mgos_upd_file_action ret = MGOS_UPDATER_SKIP_FILE; struct find_part_info find_part_info = {fi->name, &part_name, &ctx->cur_part}; ctx->cur_part.len = part_name.len = 0; json_walk(ctx->parts->ptr, ctx->parts->len, find_part, &find_part_info); if (ctx->cur_part.len == 0) return ret; /* Drop any indexes from part name, we'll add our own. */ while (1) { char c = part_name.p[part_name.len - 1]; if (c != '.' && !(c >= '0' && c <= '9')) break; part_name.len--; } struct json_token type = JSON_INVALID_TOKEN; const char *fname = NULL; uint32_t falloc = 0; json_scanf(ctx->cur_part.ptr, ctx->cur_part.len, "{load_addr:%u, falloc:%u, type: %T}", &ctx->app_load_addr, &falloc, &type); if (falloc == 0) falloc = fi->size; if (tcmp(&type, "app") == 0) { struct boot_cfg cur_cfg; int r = read_boot_cfg(ctx->cur_boot_cfg_idx, &cur_cfg); if (r < 0) { ctx->status_msg = "Could not read current boot cfg"; return MGOS_UPDATER_ABORT; } #if CC3200_SAFE_CODE_UPDATE /* * When safe code update is enabled, we write code to a new file. * Otherwise we write to the same slot we're using currently, which is * unsafe, makes reverting code update not possible, but saves space. */ create_fname( mg_mk_str_n(cur_cfg.app_image_file, strlen(cur_cfg.app_image_file) - 2), ctx->new_boot_cfg_idx, ctx->app_image_file, sizeof(ctx->app_image_file)); #else { strncpy(ctx->app_image_file, cur_cfg.app_image_file, sizeof(ctx->app_image_file)); } #endif if (ctx->app_load_addr >= 0x20000000) { fname = ctx->app_image_file; } else { ctx->status_msg = "Bad/missing app load_addr"; ret = MGOS_UPDATER_ABORT; } } else if (tcmp(&type, "fs") == 0) { json_scanf( ctx->cur_part.ptr, ctx->cur_part.len, "{fs_size: %u, fs_block_size: %u, fs_page_size: %u, fs_erase_size: %u}", &ctx->fs_size, &ctx->fs_block_size, &ctx->fs_page_size, &ctx->fs_erase_size); if (ctx->fs_size > 0 && ctx->fs_block_size > 0 && ctx->fs_page_size > 0 && ctx->fs_erase_size > 0) { char fs_container_prefix[MAX_FS_CONTAINER_PREFIX_LEN]; create_fname(part_name, ctx->new_boot_cfg_idx, fs_container_prefix, sizeof(fs_container_prefix)); /* Delete container 1 (if any) so that 0 is the only one. */ cc32xx_vfs_dev_slfs_container_delete_container(fs_container_prefix, 1); cc32xx_vfs_dev_slfs_container_fname(fs_container_prefix, 0, (_u8 *) ctx->fs_container_file); fname = ctx->fs_container_file; if (fi->size > ctx->fs_size) { /* Assume meta has already been added. */ falloc = fi->size; } else { falloc = FS_CONTAINER_SIZE(fi->size); } } else { ctx->status_msg = "Missing FS parameters"; ret = MGOS_UPDATER_ABORT; } } if (fname != NULL) { int r = prepare_to_write(ctx, fi, fname, falloc, &ctx->cur_part); if (r < 0) { LOG(LL_ERROR, ("err = %d", r)); ret = MGOS_UPDATER_ABORT; } else { ret = (r > 0 ? MGOS_UPDATER_PROCESS_FILE : MGOS_UPDATER_SKIP_FILE); } } if (ret == MGOS_UPDATER_SKIP_FILE) { DBG(("Skipping %s %.*s", fi->name, (int) part_name.len, part_name.p)); } return ret; } int mgos_upd_file_data(struct mgos_upd_hal_ctx *ctx, const struct mgos_upd_file_info *fi, struct mg_str data) { _i32 r = sl_FsWrite(ctx->cur_fh, fi->processed, (_u8 *) data.p, data.len); if (r != data.len) { ctx->status_msg = "Write failed"; r = -1; } return r; }