static void cmd_threads(BaseChannel *chp, int argc, char *argv[]) { static const char *states[] = { "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSG", "WTMSG", "FINAL" }; Thread *tp; char buf[60]; (void)argv; if (argc > 0) { shellPrintLine(chp, "Usage: threads"); return; } shellPrintLine(chp, " addr stack prio refs state time"); tp = chRegFirstThread(); do { siprintf(buf, "%8lx %8lx %4u %4i %9s %u", (uint32_t)tp, (uint32_t)tp->p_ctx.r13, (unsigned int)tp->p_prio, tp->p_refs - 1, states[tp->p_state], (unsigned int)tp->p_time); shellPrintLine(chp, buf); tp = chRegNextThread(tp); } while (tp != NULL); }
static FRESULT scan_files(char *path) { FRESULT res; FILINFO fno; DIR dir; int i; char *fn; res = f_opendir(&dir, path); if (res == FR_OK) { i = strlen(path); for (;;) { res = f_readdir(&dir, &fno); if (res != FR_OK || fno.fname[0] == 0) break; if (fno.fname[0] == '.') continue; fn = fno.fname; if (fno.fattrib & AM_DIR) { siprintf(&path[i], "/%s", fn); res = scan_files(path); if (res != FR_OK) break; path[i] = 0; } else { iprintf("%s/%s\r\n", path, fn); } } } return res; }
static void cmd_tree(BaseChannel *chp, int argc, char *argv[]) { FRESULT err; uint32_t clusters; FATFS *fsp; (void)argv; if (argc > 0) { shellPrintLine(chp, "Usage: tree"); return; } if (!fs_ready) { shellPrintLine(chp, "File System not mounted"); return; } err = f_getfree("/", &clusters, &fsp); if (err != FR_OK) { shellPrintLine(chp, "FS: f_getfree() failed"); return; } siprintf((void *)fbuff, "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free", clusters, (uint32_t)MMC_FS.csize, clusters * (uint32_t)MMC_FS.csize * (uint32_t)MMC_SECTOR_SIZE); shellPrintLine(chp, (void *)fbuff); fbuff[0] = 0; scan_files((char *)fbuff); }
void main(void) { siprintf(msg, "Hello %s", "World!\n"); write(0, msg, strlen(msg)); u_pass(); }
static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) { size_t n, size; char buf[52]; (void)argv; if (argc > 0) { shellPrintLine(chp, "Usage: mem"); return; } n = chHeapStatus(NULL, &size); siprintf(buf, "core free memory : %lu bytes", chCoreStatus()); shellPrintLine(chp, buf); siprintf(buf, "heap fragments : %lu", n); shellPrintLine(chp, buf); siprintf(buf, "heap free total : %lu bytes", size); shellPrintLine(chp, buf); }
/*------------------------------------------------------------------- TCP Server Task -------------------------------------------------------------------*/ void TcpServerTask(void * pd) { int ListenPort = (int) pd; // Set up the listening TCP socket int fdListen = listen(INADDR_ANY, ListenPort, 5); if (fdListen > 0) { IPADDR client_addr; WORD port; while(1) { // The accept() function will block until a TCP client requests // a connection. Once a client connection is accepting, the // file descriptor fdnet is used to read/write to it. iprintf( "Wainting for connection on port %d...\n", ListenPort ); int fdnet = accept(fdListen, &client_addr, &port, 0); iprintf("Connected to: "); ShowIP(client_addr); iprintf(":%d\n", port); writestring(fdnet, "Welcome to the NetBurner TCP Server\r\n"); char s[20]; IPtoString(EthernetIP, s); siprintf(RXBuffer, "You are connected to IP Address %s, port %d\r\n", s, TCP_LISTEN_PORT); writestring(fdnet, RXBuffer); while (fdnet > 0) { /* Loop while connection is valid. The read() function will return 0 or a negative number if the client closes the connection, so we test the return value in the loop. Note: you can also use ReadWithTimout() in place of read to enable the connection to terminate after a period of inactivity. */ int n = 0; do { n = read( fdnet, RXBuffer, RX_BUFSIZE ); RXBuffer[n] = '\0'; iprintf( "Read %d bytes: %s\n", n, RXBuffer ); } while ( n > 0 ); // Don't foreget to close ! iprintf("Closing client connection: "); ShowIP(client_addr); iprintf(":%d\n", port); close(fdnet); fdnet = 0; } } // while(1) } // while listen }
int db_printf2(char* format, int i1, int i2) { char buf[200]; int res = siprintf(buf, format, i1, i2); if (res == -1) { iprintf("ERROR - Debug call to sprintf failed\n"); exit(-1); } db_output(buf); return res; }
static void oscNameSpaceQueryRangeEndpoint(OscChannel ch, char *fulladdr, const OscNode* node) { OscRange r; char *endoforiginal = fulladdr + strlen(fulladdr); oscNumberMatch("*", node->rangeOffset, node->range, &r); while (oscRangeHasNext(&r)) { siprintf(endoforiginal, "/%d", oscRangeNext(&r)); oscCreateMessage(ch, fulladdr, 0, 0); } }
void OpenFileRoutine(struct FileRoutines* OpenOnboardSD) { char File_name[20]={0}; (*OpenOnboardSD).drv =OpenOnBoardFlash(); int *card_status = initOnBoardSD((*OpenOnboardSD).drv); uint8_t n=card_status[1] + 1; siprintf(File_name, "LOG%d.txt", n); (*OpenOnboardSD).fp = f_open(File_name, "w+"); }
void saveFile(const char* filename, u8*data, uint32 len) { FILE * fp = fopen(filename,"wb"); if(fp == NULL) { char tmp[128]; siprintf(tmp,"File failed to load!\nFile: %s",filename); sassert(fp != NULL,tmp); } file_size = fwrite (data, 1, len, fp); fclose(fp); }
u8 * loadFile(const char* filename) { // size_t filesize = 0; { struct stat results; stat(filename, &results);// == 0 file_size = results.st_size; if(file_size >= 1024*1024*2) { char tmp[128]; siprintf(tmp,"Error!\n File is too large to load.\n Please use segment loader.\n" "File: %s",filename); sassert(file_size < 1024*1024*2,tmp); } } FILE * fp = fopen(filename,"rb"); if(fp == NULL) { //Try again! //fp = fopen(filename,"rb"); char tmp[128]; siprintf(tmp,"File failed to load!\nFile: %s",filename); sassert(fp != NULL,tmp); } u8 *data = (u8*)malloc(file_size*sizeof(u8)); sassert(data != NULL,"Out of memory!"); //read data to memory size_t sz = fread(data,1,file_size,fp); sassert(sz == file_size,"Reading Failed!"); fclose(fp); return data; // return loadFileX(filename); }
/* build hue bridge ID in form of mac0:mac1:mac2:fffe:mac3:mac4:mac5 */ uint32_t http_build_bridgeid(char *outstr, int32_t size) { uint32_t offset = 0; uint8_t *mac; *outstr = '\0'; if(netif_default) { mac = netif_default->hwaddr; offset += siprintf(outstr, size, "%02x%02x%02xFFFE%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } return offset; }
void SendConfigReport(int ws_fd) { SMPoolPtr pq; int dataLen = nun.Serialize((char *)pq->pData, ETHER_BUFFER_SIZE); dataLen = siprintf(ReportBuffer, "{ \"stepper\": { \"speed\": %d, \"dir\": %d, \"forever\": %s, " "\"rem\": %d, \"taken\": %ld }, \"range\": %u, \"nun\": %s }", stepSpeed, stepDir, (stepForever ? "true" : "false"), Steppers[1].stepsRem, Steppers[1].stepsTaken, lidarRange, (char *)pq->pData); writeall(ws_fd, ReportBuffer, dataLen); }
/* build UUID from MAC address in form of 2f402f80-da50-1111-9933-00118877ff55 */ uint32_t http_build_uuid(char *outstr, uint32_t size) { uint32_t offset = 0; uint8_t *mac; *outstr = '\0'; if(netif_default) { /* build location */ mac = netif_default->hwaddr; offset += siprintf(outstr, size, "2f402f80-da50-1111-9933-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } return offset; }
/* * Dispatch data to matching nodes. * Range nodes are treated specially - check if the direct child has * a matching handler & trigger it. Otherwise, descend looking for a * child with a matching handler by name only. */ bool oscDispatchNode(OscChannel ch, char* addr, char* fulladdr, const OscNode* node, OscData data[], int datalen) { char* nextPattern = strchr(addr, '/'); if (nextPattern != 0) *nextPattern++ = 0; if (node->handler != NULL) { node->handler(ch, fulladdr, 0, data, datalen); return true; } uint8_t i; if (node->range > 0) { // as part of our cheat, ranges can only be the second to last node. // we jump down a level here since we are planning on getting to the handler // without traversing the tree any further for (i = 0; node->children[i] != 0; i++) { if (node->children[i]->handler && oscPatternMatch(nextPattern, node->children[i]->name)) { OscRange r; if (oscNumberMatch(addr, node->rangeOffset, node->range, &r)) { *(addr - 1) = 0; char *endofaddr = fulladdr + strlen(fulladdr); while (oscRangeHasNext(&r)) { int idx = oscRangeNext(&r); // recreate an address specific to this index, in the case that we got here // through a pattern match siprintf(endofaddr, "/%d/%s", idx, node->children[i]->name); node->children[i]->handler(ch, fulladdr, idx, data, datalen); } return true; } } } // replace the nulls we stuck in the address string *--addr = '/'; *(nextPattern - 1) = '/'; } // otherwise, go down to the next level and try some more for (i = 0; node->children[i] != 0; ++i) { if (oscPatternMatch(addr, node->children[i]->name)) { *(nextPattern - 1) = '/'; // replace this - we nulled it earlier if (oscDispatchNode(ch, nextPattern, fulladdr, node->children[i], data, datalen)) return true; } } return false; }
int main() { int hwVerMajor, hwVerMinor; char buffer[100]; // Separate major/minor version hwVerMajor = Dataflash_info.hwVersion / 100; hwVerMinor = Dataflash_info.hwVersion % 100; // Build and blit text siprintf(buffer, "Hw: %d.%02d\n%s\nFlip: %d\nBoot: %s", hwVerMajor, hwVerMinor, Display_GetType() == DISPLAY_SSD1327 ? "SSD1327" : "SSD1306", Display_IsFlipped(), Dataflash_info.bootFlag == DATAFLASH_BOOTFLAG_LDROM ? "LD" : "AP"); Display_PutText(0, 0, buffer, FONT_DEJAVU_8PT); // Update display Display_Update(); }
void setupArgV() { //Return if we are on an emulator! if (memcmp(&__NDSHeader->filenameOffset, &((tNDSHeader *)0x08000000)->filenameOffset, 16) == 0 ) { NOCASH = true; //Kill argv for desmume compatibility __system_argv->argvMagic = 0; return; } //Let use make an updated version of this now shall we? struct __argv *newArg = ((struct __argv*)0x02FFFE70); struct __argv *oldArg = ((struct __argv*)0x027FFF70); char nCommandline[MAXPATHLEN]; memset(nCommandline,0,MAXPATHLEN); if(newArg->argvMagic == ARGV_MAGIC) { //It supports the new argv, good! if(strncmp(newArg->argv[0],"fat",3) == 0) { //Everything is fine here, move along, nothing to see here. return; } BAD_ARGV = true; if(newArg->commandLine[0] != '/') { siprintf(nCommandline,"fat:%s",newArg->commandLine); } else { siprintf(nCommandline,"fat:/%s",newArg->commandLine); } newArg->length = strlen(nCommandline); newArg->commandLine = (char*)malloc(newArg->length); strcpy(newArg->commandLine,nCommandline); newArg->argc = 1; newArg->argv = &newArg->commandLine; return; } else if(oldArg->argvMagic == ARGV_MAGIC) { OLD_ARGV = true; //It uses the old argv, booo! newArg->argvMagic = ARGV_MAGIC; if(strncmp(oldArg->argv[0],"fat",3) == 0) { //Copy everything over and get on with life. newArg->argc = oldArg->argc; newArg->length = oldArg->length; newArg->commandLine = oldArg->commandLine; newArg->argv = oldArg->argv; return; } BAD_ARGV = true; //Otherwise... ugh if(oldArg->commandLine[0] != '/') { siprintf(nCommandline,"fat:%s",oldArg->commandLine); } else { siprintf(nCommandline,"fat:/%s",oldArg->commandLine); } newArg->length = strlen(nCommandline); newArg->commandLine = (char*)malloc(newArg->length); strcpy(newArg->commandLine,nCommandline); newArg->argc = 1; newArg->argv = &newArg->commandLine; return; } NO_ARGV = true; //Otherwise... oi, try compatibility //this is DANGEROUS (be sure to add extra checks!) siprintf(nCommandline, "fat:/"APP_ARTIFACT); newArg->argvMagic = ARGV_MAGIC; newArg->length = strlen(nCommandline); newArg->commandLine = (char*)malloc(newArg->length); strcpy(newArg->commandLine,nCommandline); newArg->argc = 1; newArg->argv = &newArg->commandLine; }
void printNumber(char *buff, int32_t temperature) { siprintf(buff, "%"PRIu32, temperature); }
//// UploadKickstart() //// char UploadKickstart(char *name) { int keysize=0; char filename[12]; strncpy(filename, name, 8); // copy base name strcpy(&filename[8], "ROM"); // add extension BootPrint("Checking for Amiga Forever key file:"); if(FileOpen(&file,"ROM KEY")) { keysize=file.size; if(file.size<sizeof(romkey)) { int c=0; while(c<keysize) { FileRead(&file, &romkey[c]); c+=512; FileNextSector(&file); } BootPrint("Loaded Amiga Forever key file"); } else { BootPrint("Amiga Forever keyfile is too large!"); } } BootPrint("Loading file: "); BootPrint(filename); if(minimig_v1()) { if (RAOpen(&romfile, filename)) { if (romfile.size == 0x80000) { // 512KB Kickstart ROM BootPrint("Uploading 512 KB Kickstart..."); PrepareBootUpload(0xF8, 0x08); SendFile(&romfile); return(1); } else if ((romfile.size == 0x8000b) && keysize) { // 512KB Kickstart ROM BootPrint("Uploading 512 KB Kickstart (Probably Amiga Forever encrypted...)"); PrepareBootUpload(0xF8, 0x08); SendFileEncrypted(&romfile,romkey,keysize); return(1); } else if (romfile.size == 0x40000) { // 256KB Kickstart ROM BootPrint("Uploading 256 KB Kickstart..."); PrepareBootUpload(0xF8, 0x04); SendFile(&romfile); return(1); } else if ((romfile.size == 0x4000b) && keysize) { // 256KB Kickstart ROM BootPrint("Uploading 256 KB Kickstart (Probably Amiga Forever encrypted..."); PrepareBootUpload(0xF8, 0x04); SendFileEncrypted(&romfile,romkey,keysize); return(1); } else { BootPrint("Unsupported ROM file size!"); } } else { siprintf(s, "No \"%s\" file!", filename); BootPrint(s); } } else { if (RAOpen(&romfile, filename)) { int i,j; unsigned int adr, size, base=0x180000, offset=0xc00000, data; puts("Uploading 512KB Kickstart ..."); size = ((romfile.file.size)+511)>>9; iprintf("File size: %d.%01dkB\r", romfile.file.size>>10, romfile.file.size&0x3ff); iprintf("["); for (i=0; i<size; i++) { if (!(i&31)) iprintf("*"); RARead(&romfile,sector_buffer,512); EnableOsd(); adr = 0xf80000 + i*512; SPI(OSD_CMD_WR); SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); for (j=0; j<512; j=j+4) { SPI(sector_buffer[j+0]); SPI(sector_buffer[j+1]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPI(sector_buffer[j+2]); SPI(sector_buffer[j+3]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); } DisableOsd(); } iprintf("]\r"); return(1); } }
void getString(char *buff, char *state) { siprintf(buff, "%s", state); }
/* return length of response */ static int http_serv_get(struct netconn *conn, http_parser_t *http_parser, char *responseBuf, uint16_t max_resp_size) { int responseBuf_len = 0; uint16_t len; char tmpbuf[64]; if(http_parser->num_token == 0) { /* url = "/" */ /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ memcpy(responseBuf, http_html_hdr, sizeof(http_html_hdr)-1); responseBuf_len += sizeof(http_html_hdr)-1; /* Send our HTML page */ memcpy(responseBuf, http_index_html, sizeof(http_index_html)-1); responseBuf_len += sizeof(http_index_html)-1; } else if(http_parser->val_token[0] == HUE_URL_TOKEN_DESCRIPTION_XML) { responseBuf_len = 0; memcpy(&responseBuf[responseBuf_len], http_xml_hdr, sizeof(http_xml_hdr)-1); responseBuf_len += sizeof(http_xml_hdr)-1; memcpy(&responseBuf[responseBuf_len], http_description_xml1, sizeof(http_description_xml1)-1); responseBuf_len += sizeof(http_description_xml1)-1; /* <URLBase>http://192.168.0.106:80/</URLBase> */ len = http_build_ipaddr(tmpbuf, 64); len = siprintf(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len, "<URLBase>http://%s:80/</URLBase>", tmpbuf); responseBuf_len += len; memcpy(&responseBuf[responseBuf_len], http_description_xml2, sizeof(http_description_xml2)-1); responseBuf_len += sizeof(http_description_xml2)-1; /* <UDN>uuid:2f402f80-da50-11e1-9b23-0017881733fb</UDN> */ len = http_build_uuid(tmpbuf, 64); len = siprintf(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len, "<UDN>uuid:%s</UDN>", tmpbuf); responseBuf_len += len; memcpy(&responseBuf[responseBuf_len], http_description_xml3, sizeof(http_description_xml3)-1); responseBuf_len += sizeof(http_description_xml3)-1; } else if(http_parser->val_token[0] == HUE_URL_TOKEN_API) { if((http_parser->val_token[2] == HUE_URL_TOKEN_LIGHTS) && (http_parser->val_token[3] == HUE_URL_TOKEN_NEW)) { /* Get new lights: /api/<username>/lights/new */ } else if((http_parser->val_token[2] == HUE_URL_TOKEN_LIGHTS) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* get all lights: /api/<username>/lights */ responseBuf_len += process_hue_api_get_all_lights(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_LIGHTS) && (http_parser->num_token == 4)) { uint16_t light_id; light_id = strtol(http_parser->url_token[3], NULL, 10); memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get light attributes and state: /api/<username>/lights/<id> */ responseBuf_len += process_hue_api_get_light_attr_state(light_id, &responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_CONFIG)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* get configuration: /api/<username>/config */ responseBuf_len += process_hue_api_get_configuration(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_GROUPS) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get all groups: /api/<username>/groups */ responseBuf_len += process_hue_api_get_all_groups(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_SCHEDULES) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get all schedules: /api/<username>/schedules */ responseBuf_len += process_hue_api_get_all_schedules(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_SCENES) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get all scenes: /api/<username>/scenes */ responseBuf_len += process_hue_api_get_all_scenes(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_SENSORS) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get all sensors: /api/<username>/sensors */ responseBuf_len += process_hue_api_get_all_sensors(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_RULES) && (http_parser->num_token == 3)) { memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get all sensors: /api/<username>/rules */ responseBuf_len += process_hue_api_get_all_rules(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if((http_parser->val_token[2] == HUE_URL_TOKEN_GROUPS) && (http_parser->num_token == 4)) { uint32_t group_id; memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; /* Get group attributes: /api/<username>/groups/<id> */ group_id = strtol(http_parser->url_token[3], NULL, 10); responseBuf_len += process_hue_api_get_group_attr(group_id, &responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } else if(http_parser->num_token == 2) { /* Get full state (datastore): /api/<username> */ memcpy(&responseBuf[responseBuf_len], http_json_hdr, sizeof(http_json_hdr)-1); responseBuf_len += sizeof(http_json_hdr)-1; responseBuf_len += process_hue_api_get_full_state(&responseBuf[responseBuf_len], max_resp_size-responseBuf_len); } } assert(responseBuf_len< max_resp_size); return responseBuf_len; }
void ftp_cmd_PWD(int s, char* cmd, char* arg) { siprintf(tmpStr, "\"%s\"",currentPath); ftp_sendResponse(s, 257, tmpStr); }
/*------------------------------------------------------------------- Convert IP address to a string -------------------------------------------------------------------*/ void IPtoString(IPADDR ia, char *s) { PBYTE ipb= (PBYTE)&ia; siprintf(s, "%d.%d.%d.%d",(int)ipb[0],(int)ipb[1],(int)ipb[2],(int)ipb[3]); }
/*------------------------------------------------------------------- TCP Server Task -------------------------------------------------------------------*/ void TcpServerTask(void * pd) { int ListenPort = (int) pd; // Set up the listening TCP socket int fdListen = listen(INADDR_ANY, ListenPort, 5); if (fdListen > 0) { IPADDR client_addr; WORD port; while(1) { // The accept() function will block until a TCP client requests // a connection. Once a client connection is accepting, the // file descriptor fdnet is used to read/write to it. iprintf( "Wainting for connection on port %d...\n", ListenPort ); int fdnet = accept(fdListen, &client_addr, &port, 0); iprintf("Connected to: "); ShowIP(client_addr); iprintf(":%d\n", port); writestring(fdnet, "Welcome to the NetBurner TCP Server\r\n"); char s[20]; IPtoString(EthernetIP, s); siprintf(RXBuffer, "You are connected to IP Address %s, port %d\r\n", s, TCP_LISTEN_PORT); writestring(fdnet, RXBuffer); int tcp_timeout = 0; while (fdnet > 0) { // declare file descriptor sets fd_set read_fds; fd_set error_fds; // Clear all bits in fd sets FD_ZERO(&read_fds); FD_ZERO(&error_fds); // Set bits in fd sets for our fd, fdnet FD_SET(fdnet, &read_fds); FD_SET(fdnet, &error_fds); // Select will block until an event occurs and a fd set bit is // set, or a timeout occurs. A timeout is ok in this situation, // we just use it to illustrate the task is still running. if (select(FD_SETSIZE, &read_fds,(fd_set *)0, &error_fds, 30 * TICKS_PER_SECOND)) { if (FD_ISSET(fdnet,&read_fds)) // Network data available to be read { int n = read(fdnet, RXBuffer, RX_BUFSIZE); RXBuffer[n] = '\0'; iprintf("Read %d bytes: %s\n", n, RXBuffer); //writestring(fdnet, RXBuffer); } if (FD_ISSET(fdnet, &error_fds) && !FD_ISSET(fdnet, &read_fds)) // Network error condition { iprintf("Network Error, closing socket\n"); close(fdnet); fdnet = 0; } } else { // Select timed out. If timeout exceeds 10 timeout // periods, the connection will be closed. iprintf("select() timeout\n"); tcp_timeout++; if (tcp_timeout > 10) { iprintf("Connection timed out, closing socket\n"); close(fdnet); fdnet = 0; } } // Select } // While fdnet is valid } // while(1) } // while listen }
void getFloating(char *buff, int32_t floating) { siprintf(buff, "%"PRIu32".%02"PRIu32, floating / 1000, floating % 1000 / 10); }
void getFloatingTenth(char *buff, uint32_t floating) { siprintf(buff, "%"PRIu32".%01"PRIu32, floating / 1000, floating % 1000 / 100); }
static void netloader_socket_error(const char *func, int err) { siprintf(errbuf, " %s: err=%d", func, err); netloader_deactivate(); }
/* Will always show 3 decimals, todo: make the '3' a param */ void formatFixedPoint(int32_t value, int32_t divisor, char *formatted) { if(divisor == 0) siprintf(formatted, "infin"); else siprintf(formatted, "%"PRId32".%03"PRId32, value/divisor, value % divisor); }
void getPercent(char *buff, int8_t percent) { siprintf(buff, "%d%%", percent); }
void StateCoreNameSet(const char* str) { siprintf(lastcorename, "%s", str); }