Code APP3

Dependencies:   mbed EthernetInterface WebSocketClient mbed-rtos BufferedSerial

Fork of APP3_Lab by Jean-Philippe Fournier

parser.cpp

Committer:
Cheroukee
Date:
2017-10-01
Revision:
13:5f21dd134bd2
Parent:
6:9ed8153f1328
Child:
14:cd488eba8bba

File content as of revision 13:5f21dd134bd2:

#include "parser.h"

LocalFileSystem local("local");
/*
void ReadFile()
{
    Serial pc(USBTX, USBRX); // tx, rx
    FILE *fp = fopen("/local/config.txt", "r");  // Ouvrir config.txt pour lecture seulement
    if(fp == NULL)
    {
        pc.printf("Failed to find configuration file. \n\r");
    }
    else
    {
        pc.printf("Config file opened. \n\r");
        if (fscanf(fp,"0x%x", &PanID) > 0)
        {
            pc.printf("PanId : 0x%x value : %u\r\n", PanID, PanID); // Display PanId
        }
        fscanf(fp,"%s",URL); // read URL        
        pc.printf("ServeurURL : %s\r\n",URL); // Display URL
        fclose(fp);
    }
} */

coordinator_config_t read_coordinator_config()
{
    coordinator_config_t config;

    Serial pc(USBTX, USBRX); // tx, rx
    FILE *fp = fopen("/local/config.txt", "r");  // Ouvrir config.txt pour lecture seulement
    pc.printf("Opening configuration file for the coordinator\n\r");
    if (fp == NULL)
    {
        pc.printf("Failed to find configuration file. Setting default configuration\n\r");
        
        sprintf(config.server_url, "localhost");
        config.pan_id = 0x1;
    }
    else
    {
        pc.printf("Config file opened. \n\r");
        fscanf(fp,"0x%x", &config.pan_id);        
        fscanf(fp,"%s", config.server_url); // read URL        
        fclose(fp);

        pc.printf("PanId : 0x%x value : %u\r\n", config.pan_id, config.pan_id); // Display PanId
        pc.printf("ServeurURL : %s\r\n",config.server_url); // Display URL
    }
    return config;
}

router_config_t read_router_config()
{
    router_config_t config;
    
    Serial pc(USBTX, USBRX); // tx, rx
    FILE *fp = fopen("/local/config.txt", "r");  // Ouvrir config.txt pour lecture seulement
    pc.printf("Opening configuration file for the router\n\r");
    if (fp == NULL)
    {
        pc.printf("Failed to find configuration file. Setting default configuration\n\r");
        
        config.refresh_freq = 0x1;
        config.pan_id = 0x1;
    }
    else
    {
        pc.printf("Config file opened. \n\r");
        fscanf(fp,"0x%x", &config.pan_id);
        fscanf(fp,"%u", &config.refresh_freq); // read URL   
        fclose(fp);     
        
        pc.printf("PanId : 0x%x value : %u\r\n", config.pan_id, config.pan_id); // Display PanId
        pc.printf("Sensor refresh rate : %u\r\n",config.refresh_freq); // Display URL
    }
    return config;
}