int logw_append(struct logw *lgw, struct msg *k, struct msg *v, msgtype_t t, int tbn) { int r = NESS_OK; char *base; uint32_t pos = 0; uint32_t size = + 4 /* length of beginning */ + 4 /* table number */ + CRC_SIZE; size += sizeof(k->size); size += k->size; if (v) { size += sizeof(v->size);; size += v->size; } _check_space(lgw, size); base = lgw->base; putuint32(base + pos, size); pos += 4; putuint32(base + pos, tbn); pos += 4; uint32_t fixsize = k->size; fixsize = ((fixsize << 8) | (char)t); putuint32(base + pos, fixsize); pos += 4; putnstr(base + pos, k->data, k->size); pos += k->size; if (v) { putuint32(base + pos, v->size); pos += 4; putnstr(base + pos, v->data, v->size); pos += v->size; } uint32_t xsum; /* not include the length of the header */ r = buf_xsum(base + 4, pos - 4, &xsum); if (r == 0) r = NESS_DO_XSUM_ERR; putuint32(base + pos, xsum); pos += 4; nassert(pos == size); lgw->size += size; ness_os_write(lgw->fd, base, size); return r; }
static void cread_add_char(char ichar, int insert, unsigned long *num, unsigned long *eol_num, char *buf, unsigned long len) { unsigned long wlen; /* room ??? */ if (insert || *num == *eol_num) { if (*eol_num > len - 1) { getcmd_cbeep(); return; } (*eol_num)++; } if (insert) { wlen = *eol_num - *num; if (wlen > 1) { memmove(&buf[*num+1], &buf[*num], wlen-1); } buf[*num] = ichar; putnstr(buf + *num, wlen); (*num)++; while (--wlen) { getcmd_putch(CTL_BACKSPACE); } } else { /* echo the character */ wlen = 1; buf[*num] = ichar; putnstr(buf + *num, wlen); (*num)++; } }
/*! * @brief use vsprintf for format. */ int shell_printf(const char * format,...) { int chars; va_list ap; char printbuffer[SHELL_CB_SIZE]; va_start(ap, format); chars = vsnprintf(printbuffer, SHELL_CB_SIZE, format, ap); //va_end(ap); /* follow MISRA... */ putnstr(printbuffer, chars); return chars ; }
static void useless_func(t_arg *arg, void **s, int size, int opt) { if (opt) { if (arg->conv == 's') arg->nb_char += putnstr(arg, *s, size); else putnwchar(arg, *((wchar_t **)s), size); make_padding(arg); } else { if (arg->mod && arg->mod[0] == 'l') arg->conv = 'S'; if (*((char **)s) == NULL) { arg->conv = 's'; *s = "(null)"; } } }
static void outnstring(Thread *, Entry_frame *r) { putnstr((char*)r->r[0], r->r[1]); }
static int cread_line(const char *const prompt, char *buf, unsigned int *len, int timeout) { unsigned long num = 0; unsigned long eol_num = 0; unsigned long wlen; char ichar; int insert = 1; int esc_len = 0; char esc_save[8]; int init_len = strlen(buf); //int first = 1; if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); while (1) { #if 0 #ifdef CONFIG_BOOT_RETRY_TIME while (!tstc()) { /* while no incoming data */ if (retry_time >= 0 && get_ticks() > endtime) return (-2); /* timed out */ // WATCHDOG_RESET(); } #endif if (first && timeout) { uint64_t etime = endtick(timeout); while (!tstc()) { /* while no incoming data */ if (get_ticks() >= etime) return -2; /* timed out */ WATCHDOG_RESET(); } first = 0; } #endif ichar = getcmd_getch(); if ((ichar == '\n') || (ichar == '\r')) { serial_putchar('\n'); break; } /* * handle standard linux xterm esc sequences for arrow key, etc. */ if (esc_len != 0) { if (esc_len == 1) { if (ichar == '[') { esc_save[esc_len] = ichar; esc_len = 2; } else { cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; } continue; } switch (ichar) { case 'D': /* <- key */ ichar = CTL_CH('b'); esc_len = 0; break; case 'C': /* -> key */ ichar = CTL_CH('f'); esc_len = 0; break; /* pass off to ^F handler */ case 'H': /* Home key */ ichar = CTL_CH('a'); esc_len = 0; break; /* pass off to ^A handler */ case 'A': /* up arrow */ ichar = CTL_CH('p'); esc_len = 0; break; /* pass off to ^P handler */ case 'B': /* down arrow */ ichar = CTL_CH('n'); esc_len = 0; break; /* pass off to ^N handler */ default: esc_save[esc_len++] = ichar; cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; continue; } } switch (ichar) { case 0x1b: if (esc_len == 0) { esc_save[esc_len] = ichar; esc_len = 1; } else { printf("impossible condition #876\n"); esc_len = 0; } break; case CTL_CH('a'): BEGINNING_OF_LINE(); break; case CTL_CH('c'): /* ^C - break */ *buf = '\0'; /* discard input */ return (-1); case CTL_CH('f'): if (num < eol_num) { getcmd_putch(buf[num]); num++; } break; case CTL_CH('b'): if (num) { getcmd_putch(CTL_BACKSPACE); num--; } break; case CTL_CH('d'): if (num < eol_num) { wlen = eol_num - num - 1; if (wlen) { memmove(&buf[num], &buf[num+1], wlen); putnstr(buf + num, wlen); } getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('k'): ERASE_TO_EOL(); break; case CTL_CH('e'): REFRESH_TO_EOL(); break; case CTL_CH('o'): insert = !insert; break; case CTL_CH('x'): case CTL_CH('u'): BEGINNING_OF_LINE(); ERASE_TO_EOL(); break; case DEL: case DEL7: case 8: if (num) { wlen = eol_num - num; num--; memmove(&buf[num], &buf[num+1], wlen); getcmd_putch(CTL_BACKSPACE); putnstr(buf + num, wlen); getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('p'): case CTL_CH('n'): { char * hline; esc_len = 0; if (ichar == CTL_CH('p')) hline = hist_prev(); else hline = hist_next(); if (!hline) { getcmd_cbeep(); continue; } /* nuke the current line */ /* first, go home */ BEGINNING_OF_LINE(); /* erase to end of line */ ERASE_TO_EOL(); /* copy new line into place and display */ strcpy(buf, hline); eol_num = strlen(buf); REFRESH_TO_EOL(); continue; } #ifdef CONFIG_AUTO_COMPLETE case '\t': { int num2, col; /* do not autocomplete when in the middle */ if (num < eol_num) { getcmd_cbeep(); break; } buf[num] = '\0'; col = strlen(prompt) + eol_num; num2 = num; if (cmd_auto_complete(prompt, buf, &num2, &col)) { col = num2 - num; num += col; eol_num += col; } break; } #endif default: cread_add_char(ichar, insert, &num, &eol_num, buf, *len); break; } } *len = eol_num; buf[eol_num] = '\0'; /* lose the newline */ if (buf[0] && buf[0] != CREAD_HIST_CHAR) cread_add_to_hist(buf); hist_cur = hist_add_idx; return 0; }
/*! * @brief read line into buffer */ static int cread_line(const char * const prompt, char * buf, uint8_t * len) { uint8_t num = 0; uint8_t eol_num = 0; uint8_t wlen; char ichar = 0; uint8_t insert = 1; uint8_t esc_len = 0; char esc_save[8]; uint8_t init_len = strlen(buf); if (init_len) { cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); } while (1) { do { ichar = sgetc(); }while(!ichar); if ((ichar == '\n') || (ichar == '\r')) { sputc('\r');sputc('\n'); break; } /* handle standard linux xterm esc sequences for arrow key, etc.*/ if (esc_len != 0) { if (esc_len == 1) { if (ichar == '[') { esc_save[esc_len] = ichar; esc_len = 2; } else { cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; } continue; } switch (ichar) { case 'D': /* <- key */ ichar = CTL_CH('b'); esc_len = 0; break; case 'C': /* -> key */ ichar = CTL_CH('f'); esc_len = 0; break; /* pass off to ^F handler */ case 'H': /* Home key */ ichar = CTL_CH('a'); esc_len = 0; break; /* pass off to ^A handler */ case 'A': /* up arrow */ ichar = CTL_CH('p'); esc_len = 0; break; /* pass off to ^P handler */ case 'B': /* down arrow */ ichar = CTL_CH('n'); esc_len = 0; break; /* pass off to ^N handler */ default: esc_save[esc_len++] = ichar; cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; continue; } } switch (ichar) { case 0x1b: if (esc_len == 0) { esc_save[esc_len] = ichar; esc_len = 1; } else { shell_printf("impossible condition #876\n"); esc_len = 0; } break; case CTL_CH('a'): BEGINNING_OF_LINE() break; case CTL_CH('c'): /* ^C - break */ *buf = '\0'; /* discard input */ return (-1); //break; /* have to follow MISRA */ case CTL_CH('f'): if (num < eol_num) { sputc(buf[num]); num++; } break; case CTL_CH('b'): if (num) { sputc(CTL_BACKSPACE); num--; } break; case CTL_CH('d'): if (num < eol_num) { wlen = eol_num - num - 1; if (wlen) { memmove(&buf[num], &buf[num+1], wlen); putnstr(buf + num, wlen); } sputc(' '); do { sputc(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('k'): ERASE_TO_EOL() break; case CTL_CH('e'): REFRESH_TO_EOL() break; case CTL_CH('o'): insert = !insert; break; case CTL_CH('x'): case CTL_CH('u'): BEGINNING_OF_LINE() ERASE_TO_EOL() break; case DEL: case DEL7: case 8: if (num) { wlen = eol_num - num; num--; memmove(&buf[num], &buf[num+1], wlen); sputc(CTL_BACKSPACE); putnstr(buf + num, wlen); sputc(' '); do { sputc(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('p'): case CTL_CH('n'): #ifdef SHELL_CONFIG_USE_HIST { char * hline; esc_len = 0; if (ichar == CTL_CH('p')) { hline = hist_prev(); } else { hline = hist_next(); } if (!hline) { shell_beep(); continue; } /* nuke the current line */ /* first, go home */ BEGINNING_OF_LINE() ERASE_TO_EOL() /* copy new line into place and display */ strcpy(buf, hline); eol_num = strlen(buf); REFRESH_TO_EOL() continue; //break; /* have to follow MISRA */ } #else { continue; } #endif /* SHELL_CONFIG_USE_HIST */ case '\t': #ifdef SHELL_CONFIG_AUTO_COMPLETE { uint8_t num2, col; /* do not autocomplete when in the middle */ if (num < eol_num) { shell_beep(); break; } buf[num] = '\0'; col = strlen(prompt) + eol_num; num2 = num; if (cmd_auto_complete(prompt, buf, &num2, &col)) { col = num2 - num; num += col; eol_num += col; } break; } #else { continue; } #endif /* SHELL_CONFIG_AUTO_COMPLETE */ default: cread_add_char(ichar, insert, &num, &eol_num, buf, *len); break; } } *len = eol_num; buf[eol_num] = '\0'; /* lose the newline */ #ifdef SHELL_CONFIG_USE_HIST if ((buf[0]) && (buf[0] != CREAD_HIST_CHAR)) { cread_add_to_hist(buf); } hist_cur = hist_add_idx; #endif return 0; }
// This gets called with the serial data from the BLE central. static void nus_data_handler(ble_nus_t* p_nus, uint8_t* p_data, uint16_t length) { UNUSED_PARAMETER(p_nus); // In this app, just print it to the console. putnstr((char*) p_data, length); }
void putstr(const char *str) { putnstr(str, strlen(str)); }