int cmd_module(int argc, char *argv[]) { struct module_info *header; header = (struct module_info *)pvPortMalloc(sizeof(struct module_info)); if (argc < 2) { cmd_print("\r\ni2c exists: %d",I2CEE_exists(SLAVE_ADDRESS_MODULE1)); } else { if(ustrncmp(argv[1],"write",5) == 0) { header->magic = 0x3A; header->vendor = 0x01; header->product = 0x01; header->version = 0x01; header->profile = ustrtoul(argv[2],NULL,16); header->modres = ustrtoul(argv[3],NULL,16); header->dummy2 = 0; header->crc = crcSlow((unsigned char *)header, sizeof(struct module_info)-sizeof(header->crc)); I2CEE_write(SLAVE_ADDRESS_MODULE1,(unsigned char *) header, sizeof(struct module_info), 0); vTaskDelay(100 / portTICK_RATE_MS); // there must be a delay after write or avoid I2CEE_read() } I2CEE_read(SLAVE_ADDRESS_MODULE1, (unsigned char *) header, sizeof(struct module_info), 0); cmd_print("\r\ni2c module magic: %X vendor: %X product: %X version: %X profile: %X modres: %X", header->magic, header->vendor, header->product, header->version, header->profile, header->modres); } vPortFree(header); return(0); }
//***************************************************************************** // //Command: time12 // // Set the current system time. Use format "HH:MM:SS:X" Where X is 'A' or 'P' // for AM or PM. HH is hours. MM is minutes. SS is seconds. // //***************************************************************************** int CMD_time12(int argc, char **argv) { const char *pcNext; // // Check the argument count and return errors for too many or too few. // if(argc == 1) { return(CMDLINE_TOO_FEW_ARGS); } if(argc > 2) { return(CMDLINE_TOO_MANY_ARGS); } // // Convert the user string to unsigned long hours and minutes. // g_ui32HourIdx = ustrtoul(argv[1], &pcNext, 10); g_ui32MinIdx = ustrtoul(pcNext + 1, &pcNext, 10); // // Accomodate the PM vs AM modification. All times are stored internally // as 24 hour format. // if(ustrncmp(pcNext + 1, "PM", 2) == 0) { if(g_ui32HourIdx < 12) { g_ui32HourIdx += 12; } } else { if(g_ui32HourIdx > 11) { g_ui32HourIdx -= 12; } } // // Perform the conversions to a time struct and store in the hibernate // module. Also do some minimal checking on the input data. // if((g_ui32HourIdx < 24) && (g_ui32MinIdx < 60)) { DateTimeSet(); } return(0); }
//***************************************************************************** // // Open a file and return a handle to the file, if found. Otherwise, // return NULL. // //***************************************************************************** struct fs_file * fs_open(const char *pcName) { const struct fsdata_file *psTree; struct fs_file *psFile = NULL; FIL *psFatFile = NULL; FRESULT fresult = FR_OK; // // Allocate memory for the file system structure. // psFile = mem_malloc(sizeof(struct fs_file)); if(psFile == NULL) { return(NULL); } // // See if a file on the SD card is being requested. // if(ustrncmp(pcName, "/sd/", 4) == 0) { // // Allocate memory for the Fat File system handle. // psFatFile = mem_malloc(sizeof(FIL)); if(psFatFile == NULL) { mem_free(psFile); return(NULL); } // // Attempt to open the file on the Fat File System. // fresult = f_open(psFatFile, pcName + 3, FA_READ); if(fresult == FR_OK) { psFile->data = NULL; psFile->len = 0; psFile->index = 0; psFile->pextension = psFatFile; return(psFile); } // // If we get here, we failed to find the file on the Fat File System, // so free up the Fat File system handle/object. // mem_free(psFatFile); mem_free(psFile); return(NULL); } // // Initialize the file system tree pointer to the root of the linked list. // psTree = FS_ROOT; // // Begin processing the linked list, looking for the requested file name. // while(NULL != psTree) { // // Compare the requested file "name" to the file name in the // current node. // if(ustrncmp(pcName, (char *)psTree->name, psTree->len) == 0) { // // Fill in the data pointer and length values from the // linked list node. // psFile->data = (char *)psTree->data; psFile->len = psTree->len; // // For now, we setup the read index to the end of the file, // indicating that all data has been read. // psFile->index = psTree->len; // // We are not using any file system extensions in this // application, so set the pointer to NULL. // psFile->pextension = NULL; // // Exit the loop and return the file system pointer. // break; } // // If we get here, we did not find the file at this node of the linked // list. Get the next element in the list. // psTree = psTree->next; } // // If we didn't find the file, ptTee will be NULL. Make sure we // return a NULL pointer if this happens. // if(psTree == NULL) { mem_free(psFile); psFile = NULL; } // // Return the file system pointer. // return(psFile); }
int ustrcmp(const char* s1, const char* s2) { return(ustrncmp(s1, s2, (uint32_t)-1)); }
/* fs_flist_proc: * Dialog procedure for the file selector list. */ static int fs_flist_proc(int msg, DIALOG *d, int c) { static int recurse_flag = 0; char *s = (char *) file_selector[FS_EDIT].dp; char tmp[32]; /* of s (in bytes) */ int size = (file_selector[FS_EDIT].d1 + 1) * uwidth_max(U_CURRENT); int sel = d->d1; int i, ret; int ch, count; if(msg == MSG_START) { if(!flist) { flist = (FLIST *) zc_malloc(sizeof(FLIST)); if(!flist) { *allegro_errno = ENOMEM; return D_CLOSE; } } else { for(i=0; i<flist->size; i++) if(flist->name[i]) zc_free(flist->name[i]); } flist->size = 0; replace_filename(flist->dir, s, uconvert_ascii("*.*", tmp), sizeof(flist->dir)); /* The semantics of the attributes passed to file_select_ex() is * different from that of for_each_file_ex() in one case: when * the 'd' attribute is not mentioned in the set of characters, * the other attributes are not taken into account for directories, * i.e the directories are all included. So we can't filter with * for_each_file_ex() in that case. */ if(attrb_state[ATTRB_DIREC] == ATTRB_ABSENT) /* accept all dirs */ for_each_file_ex(flist->dir, 0 , FA_LABEL, fs_flist_putter, (void *)1UL /* check */); else /* don't check */ for_each_file_ex(flist->dir, build_attrb_flag(ATTRB_SET), build_attrb_flag(ATTRB_UNSET) | FA_LABEL, fs_flist_putter, (void *)0UL); usetc(get_filename(flist->dir), 0); d->d1 = d->d2 = 0; sel = 0; } if(msg == MSG_END) { if(flist) { for(i=0; i<flist->size; i++) if(flist->name[i]) zc_free(flist->name[i]); zc_free(flist); flist = NULL; } } recurse_flag++; ret = jwin_abclist_proc(msg,d,c); /* call the parent procedure */ recurse_flag--; if(((sel != d->d1) || (ret == D_CLOSE)) && (recurse_flag == 0)) { replace_filename(s, flist->dir, flist->name[d->d1], size); /* check if we want to `cd ..' */ if((!ustrncmp(flist->name[d->d1], uconvert_ascii("..", tmp), 2)) && (ret == D_CLOSE)) { /* let's remember the previous directory */ usetc(updir, 0); i = ustrlen(flist->dir); count = 0; while(i>0) { ch = ugetat(flist->dir, i); if((ch == '/') || (ch == OTHER_PATH_SEPARATOR)) { if(++count == 2) break; } uinsert(updir, 0, ch); i--; } /* ok, we have the dirname in updir */ } else { usetc(updir, 0); } object_message(file_selector+FS_EDIT, MSG_START, 0); object_message(file_selector+FS_EDIT, MSG_DRAW, 0); if(ret == D_CLOSE) return object_message(file_selector+FS_EDIT, MSG_KEY, 0); } return ret; }
//***************************************************************************** // //! Opens a file. //! //! \param pcName points to a NULL terminated string containing the path and //! file name to open. //! //! This function opens a file and returns a handle allowing it to be read. //! //! \return Returns a valid file handle on success or NULL on failure. // //***************************************************************************** struct fs_file * fs_open(const char *pcName) { const struct fsdata_file *psTree; const struct fsdata_file *psEnd = NULL; struct fs_file *psFile = NULL; fs_wrapper_data *psWrapper; FRESULT fresult = FR_OK; bool bPosInd = false; char *pcFSFilename; char *pcFilename; uint32_t ui32Length; // // Allocate memory for the file system structure. // psFile = mem_malloc(sizeof(struct fs_file)); if(NULL == psFile) { return(NULL); } // // Allocate memory for our internal control structure. // psFile->pextension = mem_malloc(sizeof(fs_wrapper_data)); psWrapper = (fs_wrapper_data *)psFile->pextension; if(NULL == psWrapper) { return(NULL); } // // Find which mount point we need to use to satisfy this file open request. // psWrapper->ui32MountIndex = fs_find_mount_index(pcName, &pcFSFilename); if(psWrapper->ui32MountIndex == BAD_MOUNT_INDEX) { // // We can't map the mount index so return an error. // mem_free(psWrapper); mem_free(psFile); return(NULL); } // // Enable access to the physical medium if we have been provided with // a callback for this. // if(g_psMountPoints[psWrapper->ui32MountIndex].pfnEnable) { g_psMountPoints[psWrapper->ui32MountIndex]. pfnEnable(psWrapper->ui32MountIndex); } // // Are we opening a file on an internal file system image? // if(g_psMountPoints[psWrapper->ui32MountIndex].pui8FSImage) { // // Initialize the file system tree pointer to the root of the linked // list for this mount point's file system image. // psTree = ((const struct fsdata_file *) g_psMountPoints[psWrapper->ui32MountIndex].pui8FSImage); // // Which type of file system are we dealing with? // if(psTree->next == FILE_SYSTEM_MARKER) { // // If we found the marker, this is a position independent file // system image. Remember this and fix up the pointer to the // first descriptor by skipping over the 4 byte marker and the // 4 byte image size entry. We also keep track of where the file // system image ends since this allows us to do a bit more error // checking later. // bPosInd = true; ui32Length = *(uint32_t *)((uint8_t *)psTree + 4); psTree = (struct fsdata_file *)((int8_t *)psTree + 8); psEnd = (struct fsdata_file *)((int8_t *)psTree + ui32Length); } // // Begin processing the linked list, looking for the requested file // name. // while(NULL != psTree) { // // Compare the requested file "name" to the file name in the // current node. // if(ustrncmp(pcFSFilename, FS_POINTER(psTree, psTree->name, bPosInd), psTree->len) == 0) { // // Fill in the data pointer and length values from the // linked list node. // psFile->data = FS_POINTER(psTree, psTree->data, bPosInd); psFile->len = psTree->len; // // For now, we setup the read index to the end of the file, // indicating that all data has been read. This indicates that // all the data is currently available in a contiguous block // of memory (which is always the case with an internal file // system image). // psFile->index = psTree->len; // // We are not using a FAT file system file and don't need to // remap the filename so set these pointers to NULL. // psWrapper->psFATFile = NULL; // // Exit the loop and return the file system pointer. // break; } // // If we get here, we did not find the file at this node of the // linked list. Get the next element in the list. We can't just // assign psTree from psTree->next since this will give us the // wrong pointer for a position independent image (where the values // in the structure are offsets from the start of the file // descriptor, not absolute pointers) but we do know that a 0 in // the "next" field does indicate that this is the last file so we // can use that info to force the loop to exit at the end. // if(psTree->next == 0) { psTree = NULL; } else { psTree = (struct fsdata_file *)FS_POINTER(psTree, psTree->next, bPosInd); // // If this is a position independent file system image, we can // also check that the new node is within the image. If it // isn't, the image is corrupted to stop the search. // if(bPosInd && (psTree >= psEnd)) { psTree = NULL; } } } // // If we didn't find the file, ptTee will be NULL. Make sure we // return a NULL pointer if this happens. // if(NULL == psTree) { mem_free(psFile->pextension); mem_free(psFile); psFile = NULL; } } else { // // This file is on the FAT file system. // // // Allocate memory for the Fat File system handle. // psWrapper->psFATFile = mem_malloc(sizeof(FIL)); if(NULL == psWrapper->psFATFile) { mem_free(psFile->pextension); mem_free(psFile); psFile = NULL; } else { // // Reformat the filename to start with the FAT logical drive // number. // ui32Length = ustrlen(pcFSFilename) + 16; pcFilename = mem_malloc(ui32Length); if(!pcFilename) { // // Can't allocate temporary storage for the reformatted // filename! // mem_free(psWrapper->psFATFile); mem_free(psFile->pextension); mem_free(psFile); psFile = NULL; } else { usnprintf(pcFilename, ui32Length, "%d:%s", g_psMountPoints[psWrapper->ui32MountIndex]. ui32DriveNum, pcFSFilename); // // Attempt to open the file on the Fat File System. // fresult = f_open(psWrapper->psFATFile, pcFilename, FA_READ); // // Free the filename storage // mem_free(pcFilename); // // Did we open the file correctly? // if(FR_OK == fresult) { // // Yes - fill in the file structure to indicate that a // FAT file is in use. // psFile->data = NULL; psFile->len = 0; psFile->index = 0; } else { // // If we get here, we failed to find the file on the FAT // file system so free up the FAT handle/object. // mem_free(psWrapper->psFATFile); mem_free(psWrapper); mem_free(psFile); psFile = NULL; } } } } // // Disable access to the physical medium if we have been provided with // a callback for this. // if(g_psMountPoints[psWrapper->ui32MountIndex].pfnDisable) { g_psMountPoints[psWrapper->ui32MountIndex]. pfnDisable(psWrapper->ui32MountIndex); } return(psFile); }
//***************************************************************************** // // Given a filename, this function determine which of the configured mount // points it resides under. It returns the index of the mount point in the // g_psMountPoints array and also a pointer to the first character of the // filename with the mount point name (directory) stripped from it. // //***************************************************************************** static uint32_t fs_find_mount_index(const char *pcName, char **ppcFSFilename) { uint32_t ui32Loop; int iLenDirName; int iLenMountName; char *pcSlash; // // First extract the top level directory name which we need to match // with the mount point name. For this to exist, the pcName string // must start with a '/' character and must contain at least one more // '/'. // if(pcName[0] == '/') { // // The string starts with a '/'. Does it contain a second one? // pcSlash = strchr(pcName + 1, '/'); // // Did we find another forward slash character? // if(pcSlash) { // // Yes - the mount point name is between the start of the // string and the slash we just found. How long is this string? // iLenDirName = (int)(pcSlash - (pcName + 1)); } else { // // The mount point name is the whole string. // iLenDirName = ustrlen(pcName + 1); pcSlash = (char *)pcName + 1 + iLenDirName; } // // Now figure out which, if any, of the mount points this matches. // for(ui32Loop = 0; ui32Loop < g_ui32NumMountPoints; ui32Loop++) { // // Skip the default mount point if found. // if(!g_psMountPoints[ui32Loop].pcNamePrefix) { continue; } // // How long is the name of this mount point? // iLenMountName = ustrlen(g_psMountPoints[ui32Loop].pcNamePrefix); // // Does the mount point name match the directory name extracted // from the passed pcName? // if(iLenMountName == iLenDirName) { // // The lengths match but are the strings the same? // if(!ustrncmp(g_psMountPoints[ui32Loop].pcNamePrefix, pcName + 1, iLenDirName)) { // // Yes - we have a match. Set the stripped filename to // the second '/' and return the mount point index. // *ppcFSFilename = pcSlash; return(ui32Loop); } } } } // // If we drop out of the loop, we didn't find a specific mount point for // this file so just return the filename passed and the default mount // point. // *ppcFSFilename = (char *)pcName; return(g_ui32DefaultMountIndex); }
/* fs_flist_proc: * Dialog procedure for the file selector list. */ static int fs_flist_proc(int msg, DIALOG *d, int c) { static int recurse_flag = 0; char *s = file_selector[FS_EDIT].dp; char tmp[32]; int sel = d->d1; int i, ret; int ch, count; if (msg == MSG_START) { if (!flist) { flist = malloc(sizeof(FLIST)); if (!flist) { *allegro_errno = ENOMEM; return D_CLOSE; } } else { for (i=0; i<flist->size; i++) if (flist->name[i]) free(flist->name[i]); } flist->size = 0; replace_filename(flist->dir, s, uconvert_ascii("*.*", tmp), sizeof(flist->dir)); for_each_file(flist->dir, FA_RDONLY | FA_DIREC | FA_ARCH | FA_HIDDEN | FA_SYSTEM, fs_flist_putter, 0); if (*allegro_errno) raine_alert("", "Disk error", NULL, NULL, "OK", NULL, 13, 0); usetc(get_filename(flist->dir), 0); d->d1 = d->d2 = 0; sel = 0; } if (msg == MSG_END) { if (flist) { for (i=0; i<flist->size; i++) if (flist->name[i]) free(flist->name[i]); free(flist); flist = NULL; } } recurse_flag++; ret = d_text_list_proc(msg, d, c); /* call the parent procedure */ recurse_flag--; if (((sel != d->d1) || (ret == D_CLOSE)) && (recurse_flag == 0)) { replace_filename(s, flist->dir, flist->name[d->d1], 512); /* check if we want to `cd ..' */ if ((!ustrncmp(flist->name[d->d1], "..", 2)) && (ret == D_CLOSE)) { /* let's remember the previous directory */ ustrcpy(updir, empty_string); i = ustrlen(flist->dir); count = 0; while (i>0) { ch = ugetat(flist->dir, i); if ((ch == '/') || (ch == OTHER_PATH_SEPARATOR)) { if (++count == 2) break; } uinsert(updir, 0, ch); i--; } /* ok, we have the dirname in updir */ } else { ustrcpy(updir, empty_string); } scare_mouse(); SEND_MESSAGE(file_selector+FS_EDIT, MSG_START, 0); SEND_MESSAGE(file_selector+FS_EDIT, MSG_DRAW, 0); unscare_mouse(); if (ret == D_CLOSE) return SEND_MESSAGE(file_selector+FS_EDIT, MSG_KEY, 0); } return ret; }
//***************************************************************************** // // Fill out the psWeatherReport structure from data returned from the JSON // query. // //***************************************************************************** int32_t JSONParseCurrent(uint32_t ui32Index, tWeatherReport *psWeatherReport, struct pbuf *psBuf) { tBufPtr sBufPtr, sBufMain, sBufTemp; char pcIcon[3]; char pcCode[4]; int32_t i32Items, i32Code; // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Check and see if the request was valid. // if(GetField("cod", &sBufPtr) != 0) { sBufTemp = sBufPtr; // // Check for a 404 not found error. // i32Code = GetFieldValueInt(&sBufTemp); sBufTemp = sBufPtr; if(i32Code != INVALID_INT) { if(i32Code == 404) { return(-1); } } else if(GetFieldValueString(&sBufTemp, pcCode, sizeof(pcCode)) >= 0) { // // Check for a 404 not found error. // if(ustrncmp(pcCode, "404", 3) == 0) { return(-1); } } } // // Initialize the number of items that have been found. // i32Items = 0; // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Find the basic weather description(weather.description). // if(GetField("weather", &sBufPtr) != 0) { if(GetField("icon", &sBufPtr) != 0) { if(GetFieldValueString(&sBufPtr, pcIcon, sizeof(pcIcon)) > 0) { // // Save the image pointer. // GetImage(pcIcon, &psWeatherReport->pui8Image, &psWeatherReport->pcDescription); i32Items++; } else { // // No image was found. // psWeatherReport->pui8Image = 0; } } } // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Find the humidity value(main.humidity). // if(GetField("sys", &sBufPtr) != 0) { // // Save the pointer to the data. // sBufMain = sBufPtr; // // Get the sunrise time. // if(GetField("sunrise", &sBufPtr) != 0) { psWeatherReport->ui32SunRise = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->ui32SunRise = 0; } // // Restore the pointer to the data. // sBufPtr = sBufMain; // // Get the sunset time. // if(GetField("sunset", &sBufPtr) != 0) { psWeatherReport->ui32SunSet = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->ui32SunSet = 0; } } // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Get the last update time. // if(GetField("dt", &sBufPtr) != 0) { psWeatherReport->ui32Time = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->ui32Time = 0; } // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Find the humidity value(main.humidity). // if(GetField("main", &sBufPtr) != 0) { // // Save the pointer to the main data. // sBufMain = sBufPtr; if(GetField("humidity", &sBufPtr) != 0) { psWeatherReport->i32Humidity = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Humidity = INVALID_INT; } // // Reset the buffer pointer to main. // sBufPtr = sBufMain; // // Find the current temperature value. // if(GetField("temp", &sBufPtr) != 0) { psWeatherReport->i32Temp = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Temp = INVALID_INT; } // // Reset the buffer pointer to main. // sBufPtr = sBufMain; // // Find the current atmospheric pressure. // if(GetField("pressure", &sBufPtr) != 0) { psWeatherReport->i32Pressure = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Pressure = INVALID_INT; } } return(i32Items); }
//***************************************************************************** // // Fill out the psWeatherReport structure from data returned from the JSON // query. // //***************************************************************************** int32_t JSONParseForecast(uint32_t ui32Index, tWeatherReport *psWeatherReport, struct pbuf *psBuf) { tBufPtr sBufPtr, sBufList, sBufTemp; char pcCode[4]; int32_t i32Items,i32Code; // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Check and see if the request was valid. // if(GetField("cod", &sBufPtr) != 0) { // // Check for a 404 not found error. // sBufTemp = sBufPtr; // // Check for a 404 not found error. // i32Code = GetFieldValueInt(&sBufTemp); sBufTemp = sBufPtr; if(i32Code != INVALID_INT) { if(i32Code == 404) { return(-1); } } else if(GetFieldValueString(&sBufTemp, pcCode, sizeof(pcCode)) >= 0) { // // Check for a 404 not found error. // if(ustrncmp(pcCode, "404", 3) == 0) { return(-1); } } } // // Reset the pointers. // BufPtrInit(&sBufPtr, psBuf); // // Initialize the number of items found. // i32Items = 0; if(GetField("list", &sBufPtr) != 0) { // // Save the list pointer. // sBufList = sBufPtr; // // Get the humidity. // if(GetField("humidity", &sBufPtr) != 0) { psWeatherReport->i32Humidity = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Humidity = INVALID_INT; } // // Reset the pointer to the start of the list values. // sBufPtr = sBufList; // // Get the average daily pressure. // if(GetField("pressure", &sBufPtr) != 0) { psWeatherReport->i32Pressure = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Pressure = INVALID_INT; } // // Reset the pointer to the start of the list values. // sBufPtr = sBufList; // // Get the average daily temperature. // if(GetField("temp", &sBufPtr) != 0) { if(GetField("day", &sBufPtr) != 0) { psWeatherReport->i32Temp = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32Temp = INVALID_INT; } } // // Reset the pointer to the start of the list values. // sBufPtr = sBufList; // // Get the low temperature. // if(GetField("temp", &sBufPtr) != 0) { if(GetField("min", &sBufPtr) != 0) { psWeatherReport->i32TempLow = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32TempLow = INVALID_INT; } } // // Reset the pointer to the start of the list values. // sBufPtr = sBufList; // // Get the high temperature. // if(GetField("temp", &sBufPtr) != 0) { if(GetField("max", &sBufPtr) != 0) { psWeatherReport->i32TempHigh = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->i32TempHigh = INVALID_INT; } } // // Reset the pointer to the start of the list values. // sBufPtr = sBufList; if(GetField("dt", &sBufPtr) != 0) { psWeatherReport->ui32Time = GetFieldValueInt(&sBufPtr); i32Items++; } else { psWeatherReport->ui32Time = 0; } } return(i32Items); }
//***************************************************************************** // // Open a file and return a handle to the file, if found. Otherwise, // return NULL. This function also looks for special file names used to // provide specific status information or to control various subsystems. // These file names are used by the JavaScript on the "IO Control Demo 1" // example web page. // //***************************************************************************** struct fs_file * fs_open(const char *pcName) { const struct fsdata_file *psTree; struct fs_file *psFile = NULL; // // Allocate memory for the file system structure. // psFile = mem_malloc(sizeof(struct fs_file)); if(psFile == NULL) { return(NULL); } // // Process request to toggle STATUS LED // if(ustrncmp(pcName, "/cgi-bin/toggle_led", 19) == 0) { static char pcBuf[4]; // // Toggle the STATUS LED // io_set_led(!io_is_led_on()); // // Get the new state of the LED // io_get_ledstate(pcBuf, 4); psFile->data = pcBuf; psFile->len = strlen(pcBuf); psFile->index = psFile->len; psFile->pextension = NULL; // // Return the psFile system pointer. // return(psFile); } // // Request for LED State? // else if(ustrncmp(pcName, "/ledstate", 9) == 0) { static char pcBuf[4]; // // Get the state of the LED // io_get_ledstate(pcBuf, 4); psFile->data = pcBuf; psFile->len = strlen(pcBuf); psFile->index = psFile->len; psFile->pextension = NULL; return(psFile); } // // Request for the animation speed? // else if(ustrncmp(pcName, "/get_speed", 10) == 0) { static char pcBuf[6]; // // Get the current animation speed as a string. // io_get_animation_speed_string(pcBuf, 6); psFile->data = pcBuf; psFile->len = strlen(pcBuf); psFile->index = psFile->len; psFile->pextension = NULL; return(psFile); } // // Set the animation speed? // else if(ustrncmp(pcName, "/cgi-bin/set_speed?percent=", 12) == 0) { static char pcBuf[6]; // // Extract the parameter and set the actual speed requested. // io_set_animation_speed_string((char*)pcName + 27); // // Get the current speed setting as a string to send back. // io_get_animation_speed_string(pcBuf, 6); psFile->data = pcBuf; psFile->len = strlen(pcBuf); psFile->index = psFile->len; psFile->pextension = NULL; return(psFile); } // // If I can't find it there, look in the rest of the main psFile system // else { // // Initialize the psFile system tree pointer to the root of the linked // list. // psTree = FS_ROOT; // // Begin processing the linked list, looking for the requested file name. // while(NULL != psTree) { // // Compare the requested file "pcName" to the file name in the // current node. // if(ustrncmp(pcName, (char *)psTree->name, psTree->len) == 0) { // // Fill in the data pointer and length values from the // linked list node. // psFile->data = (char *)psTree->data; psFile->len = psTree->len; // // For now, we setup the read index to the end of the file, // indicating that all data has been read. // psFile->index = psTree->len; // // We are not using any file system extensions in this // application, so set the pointer to NULL. // psFile->pextension = NULL; // // Exit the loop and return the file system pointer. // break; } // // If we get here, we did not find the file at this node of the // linked list. Get the next element in the list. // psTree = psTree->next; } } // // If we didn't find the file, ptTee will be NULL. Make sure we // return a NULL pointer if this happens. // if(NULL == psTree) { mem_free(psFile); psFile = NULL; } // // Return the file system pointer. // return(psFile); }
//***************************************************************************** // //! Parse a HTTP response. //! //! \param pcData is a pointer to the source string/buffer. //! \param pcResponseText is a pointer to a string that will receive the //! response text from the first line of the HTTP response. //! \param pui32NumHeaders is a pointer to a variable that will receive the //! number of headers detected in pcData. //! //! Note that this function must be called after HTTPMessageTypeSet() as it //! simply appends a header to an existing string/buffer. //! //! \return Returns the HTTP response code. If parsing error occurs, returns 0. // //***************************************************************************** uint32_t HTTPResponseParse(char *pcData, char *pcResponseText, uint32_t *pui32NumHeaders) { uint32_t ui32Response = 0; uint32_t ui32Size; // // Look for "HTTP/n.n" piece. // BufferFillToCharacter(' ', pcData, g_pcTempData, &ui32Size); pcData += (ui32Size + 1); // // Fail if not a HTTP response. // if(ustrncmp(g_pcTempData, "HTTP/", 5)) { *pcResponseText = 0; *pui32NumHeaders = 0; return 0; } // // Get the return code. // BufferFillToCharacter(' ', pcData, g_pcTempData, &ui32Size); pcData += (ui32Size + 1); // // Convert return code to unsigned long. // ui32Response = ustrtoul(g_pcTempData, 0, 10); // // Get the response text. // BufferFillToEOL(pcData, pcResponseText, &ui32Size); pcData += (ui32Size + 2); // // Parse the remainder of the packet. // *pui32NumHeaders = 0; while(*pcData) { // // Search line by line and count headers. Search until there is a blank // line, which means end of headers. // BufferFillToEOL(pcData, g_pcTempData, &ui32Size); pcData += (ui32Size + 2); // // Blank line. For the purposes of this function, we're done. // if(ui32Size == 0) { break; } else { // // Increment header counter. // *pui32NumHeaders += 1; } } return ui32Response; }