int fn_print_loop_stack(_kforth_context *context) { int *p1; for(p1=context->loop_stack; p1!=context->loop_stack_ptr; p1++) { platform_printf("%d ", *p1); } platform_printf("\r\n"); return 0; }
int fn_print_top(_kforth_context *context) { if (context->stack==context->stack_ptr) { platform_printf("EOS\r\n"); } else { platform_printf("%d ", context->stack_ptr[-1]); context->stack_ptr--; } return 0; }
int platform_mutex_unlock(Mutex* m) { if (!m) { platform_printf("%s: invalid mutex %p\n", __func__, m); return -1; } wiced_result_t rc = wiced_rtos_unlock_mutex(&m->mutex); if (rc != WICED_SUCCESS) platform_printf("%s: FAILED to unlock %p: %d\n", __func__, &m->mutex, rc); return 0; }
int fn_read(_kforth_context *context) { platform_printf("Reading From 0x%08X\r\n", STACK(0)); STACK_POP(); STACK_PUSH(0); return 0; }
int platform_semaphore_post(Semaphore* s) { if (!s) { platform_printf("%s: invalid semaphore %p\n", __func__, s); return -1; } wiced_result_t rc = wiced_rtos_set_semaphore(&s->sem); if (rc != WICED_SUCCESS) { platform_printf("%s: FAILED to post semaphore %p: %d\n", __func__, &s->sem, rc); return -1; } return 0; }
void platform_network_securedinit(Network* n, const char* ca_buf, size_t ca_size) { if (!n || !ca_buf || !ca_size) { platform_printf("%s: bad args\n", __func__); return; } wiced_result_t rc = wiced_tls_init_root_ca_certificates(ca_buf); if (rc != WICED_SUCCESS) { platform_printf("%s: root CA certificate failed to initialize: %u\n", __func__, rc); return; } n->tls_enabled = WICED_TRUE; }
void platform_semaphore_deinit(Semaphore* s) { if (!s) { platform_printf("%s: invalid semaphore %p\n", __func__, s); return; } wiced_rtos_deinit_semaphore(&s->sem); }
void platform_mutex_deinit(Mutex* m) { if (!m) { platform_printf("%s: invalid mutex %p\n", __func__, m); return; } wiced_rtos_deinit_mutex(&m->mutex); }
int platform_network_write(Network* n, unsigned char* buffer, int length, int timeout) { wiced_result_t rc = wiced_tcp_stream_write(&n->stream, buffer, length); if (rc != WICED_SUCCESS) { platform_printf("unable to write data to tcp stream, rc = %d\n", rc); return -1; } rc = wiced_tcp_stream_flush(&n->stream); if (rc != WICED_SUCCESS) { platform_printf("unable to flush tcp stream, rc = %d\n", rc); return -1; } //platform_printf("%s: successfully sent %d bytes to tcp stream\n", __func__, length); return length; }
int platform_thread_join(Thread* t, int timeout_ms) { if (!t) return -1; if (platform_semaphore_wait(&t->join_sem, timeout_ms) != 0) { platform_printf("%s: timeout waiting for join\n", __func__); return -1; } return 0; }
int platform_network_connect(Network* n, char* hostname, int port) { wiced_ip_address_t ip_address; wiced_result_t rc = -1; int attempt = 1; do { platform_printf("trying to resolve hostname (attempt %d)\n", attempt); if ((rc = wiced_hostname_lookup(hostname, &ip_address, 10000)) == WICED_SUCCESS) { platform_printf("hostname resolved\n"); break; } } while (++attempt <= 5); if (rc != WICED_SUCCESS) { platform_printf("failed to resolve ip address of %s, rc = %d\n", hostname, rc); return rc; } if (n->tls_enabled) wiced_tls_init_simple_context(&n->tls_context, hostname); /* Create a TCP socket */ rc = wiced_tcp_create_socket(&n->socket, WICED_STA_INTERFACE); if (rc != WICED_SUCCESS) { platform_printf("tcp socket creation failed, rc = %d\n", rc); return rc; } if (n->tls_enabled) wiced_tcp_enable_tls(&n->socket, &n->tls_context); rc = wiced_tcp_connect(&n->socket, &ip_address, port, 5000); if (rc != WICED_SUCCESS) { platform_printf("unable to establish connection to %s:%d, rc = %d\n", hostname, port, rc); goto exit; } rc = wiced_tcp_stream_init(&n->stream, &n->socket); if (rc != WICED_SUCCESS) { platform_printf("unable to init tcp stream, rc = %d\n", rc); goto exit; } rc = WICED_SUCCESS; exit: if (rc != WICED_SUCCESS) platform_network_disconnect(n); return rc; }
int fn_print_dictionary(_kforth_context *context) { char format_str[10]; _word_entry_const *entry; _word_list_item *word_list; int name_length; int names_per_line; int name_index; int i; // first calculate the max name length name_length=0; for(entry=(_word_entry_const*)context->dictionary; entry!=NULL; entry=(_word_entry_const*)entry->next) { i=platform_strlen(entry->name); if (i>name_length)name_length=i; } // now calculate the max number of names to print per line names_per_line=((platform_get_print_width())/(name_length+2)); // +2 for 2 spaces between names platform_sprintf(format_str, "%%-%ds", name_length+2); // "%x-s" platform_printf("Print Dictionary\r\n"); for(entry=(_word_entry_const*)context->dictionary, name_index=1; entry!=NULL; entry=(_word_entry_const*)entry->next, name_index++) { if ((name_index>0)&&((name_index%names_per_line)==0)) { platform_printf("%s\r\n", entry->name); } else { platform_printf(format_str, entry->name); } } platform_printf("\r\n\r\n"); return 0; }
void log_callback(evrythng_log_level_t level, const char* fmt, va_list vl) { char msg[512]; unsigned n = vsnprintf(msg, sizeof msg, fmt, vl); if (n >= sizeof msg) msg[sizeof msg - 1] = '\0'; switch (level) { case EVRYTHNG_LOG_ERROR: platform_printf("ERROR: "); break; case EVRYTHNG_LOG_WARNING: platform_printf("WARNING: "); break; default: case EVRYTHNG_LOG_DEBUG: platform_printf("DEBUG: "); break; } platform_printf("%s\n", msg); }
int keepalive(MQTTClient* c) { int rc = MQTT_FAILURE; if (c->keepAliveInterval == 0 || !c->isconnected) { rc = MQTT_SUCCESS; goto exit; } //printf("%s: %d, ping time left: %d, tick count = %u\n", __func__, __LINE__, TimerLeftMS(&c->ping_timer), xTaskGetTickCount()); if (platform_timer_isexpired(&c->ping_timer)) { if (!c->ping_outstanding) { Timer timer; platform_timer_init(&timer); platform_timer_countdown(&timer, 1000); int len = MQTTSerialize_pingreq(c->buf, c->buf_size); if (len > 0 && (rc = sendPacket(c, len, &timer)) == MQTT_SUCCESS) // send the ping packet { platform_timer_countdown(&c->pingresp_timer, c->command_timeout_ms); c->ping_outstanding = 1; platform_printf("sent ping request\n"); } if (len > 0 && rc != MQTT_SUCCESS) { platform_printf("%s: %d failed to send ping request, rc = %d\n", __func__, __LINE__, rc); } } } exit: return rc; }
void RunAllTests() { platform_semaphore_init(&sub_sem); CuString *output = CuStringNew(); CuSuite* suite = CuGetSuite(); CuSuiteRun(suite); CuSuiteSummary(suite, output); CuSuiteDetails(suite, output); platform_printf("%s\n", output->buffer); CuStringDelete(output); CuSuiteDelete(suite); platform_semaphore_deinit(&sub_sem); }
int platform_semaphore_wait(Semaphore* s, int timeout_ms) { if (!s) { platform_printf("%s: invalid semaphore %p\n", __func__, s); return -1; } wiced_result_t rc = wiced_rtos_get_semaphore(&s->sem, timeout_ms); if (rc != WICED_SUCCESS) { //platform_printf("%s: FAILED to wait semaphore %p: %d\n", __func__, &s->sem, rc); return -1; } return 0; }
int platform_network_read(Network* n, unsigned char* buffer, int length, int timeout) { wiced_result_t rc = wiced_tcp_stream_read(&n->stream, buffer, length, timeout); if (rc != WICED_SUCCESS) { if (rc == WICED_TIMEOUT) { return -1; } else { platform_printf("failed to read data from tcp stream, rc = %d\n", rc); return 0; } } //platform_printf("successfully read %d bytes from tcp stream\n", length); return length; }
int fn_print_dictionary_full(_kforth_context *context) { _word_entry_const *entry; _word_list_item *word_list; platform_printf("Print Dictionary\r\n"); platform_printf("dictionary at 0x%08X\r\n", (unsigned int)context->dictionary); platform_printf("ADDRESS NEXT NAME WORD_FN WORD_LIST\r\n"); for(entry=(_word_entry_const*)context->dictionary; entry!=NULL; entry=(_word_entry_const*)entry->next) { platform_printf("%08X %08X %5s %08X ", (unsigned int)entry, (unsigned int)entry->next,entry->name, (unsigned int)entry->word_fn); if (entry->word_list!=NULL) { for(word_list=(_word_list_item*)entry->word_list; word_list->type!=END_TYPE; word_list++) { platform_printf("%X:%08X ", word_list->type, (unsigned int)word_list->data); } } platform_printf("\r\n"); } return 0; }
static void test_sub_callback(const char* str_json, size_t len) { char msg[len+1]; snprintf(msg, sizeof msg, "%s", str_json); platform_printf("%s: %s\n\r", __func__, msg); platform_semaphore_post(&sub_sem); }
int fn_cr(_kforth_context *context) { platform_printf("\r\n"); return 0; }
int cycle(MQTTClient* c, Timer* timer) { Timer t; platform_timer_init(&t); int len = 0, packet_type, rc = MQTT_SUCCESS; // read the socket, see what work is due if ((packet_type = readPacket(c, timer)) == MQTT_CONNECTION_LOST) { rc = MQTT_CONNECTION_LOST; goto exit; } switch (packet_type) { case CONNACK: case PUBACK: case SUBACK: break; case PUBLISH: { MQTTString topicName; MQTTMessage msg = {0}; int intQoS; if (MQTTDeserialize_publish(&msg.dup, &intQoS, &msg.retained, &msg.id, &topicName, (unsigned char**)&msg.payload, &msg.payloadlen, c->readbuf, c->readbuf_size) != 1) goto exit; msg.qos = (enum QoS)intQoS; deliverMessage(c, &topicName, &msg); if (msg.qos != QOS0) { if (msg.qos == QOS1) len = MQTTSerialize_ack(c->buf, c->buf_size, PUBACK, 0, msg.id); else if (msg.qos == QOS2) len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREC, 0, msg.id); if (len <= 0) rc = MQTT_FAILURE; else { platform_timer_countdown(&t, c->command_timeout_ms); rc = sendPacket(c, len, &t); } if (rc == MQTT_FAILURE) goto exit; // there was a problem } break; } case PUBREC: { unsigned short mypacketid; unsigned char dup, type; platform_timer_countdown(&t, c->command_timeout_ms); if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) rc = MQTT_FAILURE; else if ((len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREL, 0, mypacketid)) <= 0) rc = MQTT_FAILURE; else if ((rc = sendPacket(c, len, &t)) != MQTT_SUCCESS) // send the PUBREL packet rc = MQTT_FAILURE; // there was a problem if (rc == MQTT_FAILURE) goto exit; // there was a problem break; } case PUBCOMP: break; case PINGRESP: c->ping_outstanding = 0; platform_printf("received ping response\n"); break; } keepalive(c); if (c->ping_outstanding && platform_timer_isexpired(&c->pingresp_timer)) { c->ping_outstanding = 0; platform_printf("ping response was not received within keepalive timeout of %d\n", c->keepAliveInterval); rc = MQTT_CONNECTION_LOST; } exit: if (rc == MQTT_SUCCESS) rc = packet_type; return rc; }
int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message) { int rc = MQTT_FAILURE; Timer timer; MQTTString topic = MQTTString_initializer; topic.cstring = (char *)topicName; int len = 0; platform_mutex_lock(&c->mutex); if (!c->isconnected) goto exit; platform_timer_init(&timer); platform_timer_countdown(&timer, c->command_timeout_ms); if (message->qos == QOS1 || message->qos == QOS2) message->id = getNextPacketId(c); len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id, topic, (unsigned char*)message->payload, message->payloadlen); if (len <= 0) goto exit; if ((rc = sendPacket(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet { goto exit; // there was a problem } if (message->qos == QOS1) { if (waitfor(c, PUBACK, &timer) == PUBACK) { unsigned short mypacketid; unsigned char dup, type; if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) { platform_printf("failed to deserialize ACK\n"); rc = MQTT_FAILURE; } } else { rc = MQTT_CONNECTION_LOST; } } else if (message->qos == QOS2) { if (waitfor(c, PUBCOMP, &timer) == PUBCOMP) { unsigned short mypacketid; unsigned char dup, type; if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) rc = MQTT_FAILURE; } else { rc = MQTT_CONNECTION_LOST; } } exit: platform_mutex_unlock(&c->mutex); return rc; }
void on_connection_lost() { platform_printf("evt lib connection lost\n"); }
int fn_write(_kforth_context *context) { platform_printf("Writing 0x%X to 0x%08X\r\n", STACK(0), STACK(-1)); STACK_POP_X(2); return 0; }
void on_connection_restored() { platform_printf("evt lib connection restored\n"); }