Exemplo n.º 1
0
static long modemctl_ioctl(struct file *filp,
			   unsigned int cmd, unsigned long arg)
{
	struct modemctl *mc = filp->private_data;
	int ret;

	mutex_lock(&mc->ctl_lock);
	switch (cmd) {
	case IOCTL_MODEM_RESET:
		ret = modem_reset(mc);
		MODEM_COUNT(mc,resets);
		break;
	case IOCTL_MODEM_START:
		ret = modem_start(mc, 0);
		break;
	case IOCTL_MODEM_RAMDUMP:
		ret = modem_start(mc, 1);
		break;
	case IOCTL_MODEM_OFF:
		ret = modem_off(mc);
		break;
	default:
		ret = -EINVAL;
	}
	mutex_unlock(&mc->ctl_lock);
	pr_info("modemctl_ioctl() %d\n", ret);
	return ret;
}
Exemplo n.º 2
0
static long modemctl_ioctl(struct file *filp,
                           unsigned int cmd, unsigned long arg)
{
    struct modemctl *mc = filp->private_data;
    struct dpram_firmware fw;
    struct stat_info *pst;
    int ret = 0;

    mutex_lock(&mc->ctl_lock);
    switch (cmd) {
    case IOCTL_MODEM_RESET:
        ret = modem_reset(mc);
        MODEM_COUNT(mc,resets);
        break;
    case IOCTL_MODEM_START:
        ret = modem_start(mc, 0);
        break;
    case IOCTL_MODEM_RAMDUMP:
        ret = modem_start(mc, 1);
        break;
    case IOCTL_MODEM_OFF:
        ret = modem_off(mc);
        break;

    /* CDMA modem update in recovery mode */
    case IOCTL_MODEM_FW_UPDATE:
        pr_info("IOCTL_MODEM_FW_UPDATE\n");
        if (arg == '\0') {
            pr_err("No firmware");
            break;
        }

        ret = copy_from_user((void *)&fw, (void *)arg, sizeof(fw));
        if (ret < 0) {
            pr_err("copy from user failed!");
            ret = -EINVAL;
        } else if (dpram_process_modem_update(mc, &fw) < 0) {
            pr_err("firmware write failed\n");
            ret = -EIO;
        }
        break;
    case IOCTL_MODEM_CHK_STAT:
        pst = (struct stat_info *)arg;
        if (mc->is_modem_delta_update)
            ret = dpram_chk_delta_update(mc, &(pst->pct), pst->msg);
        else
            ret = dpram_chk_full_update(mc, &(pst->pct), pst->msg);
        break;
    case IOCTL_MODEM_PWROFF:
        pr_info("IOCTL_MODEM_PWROFF\n");
        dpram_modem_pwroff(mc);
        break;
    default:
        ret = -EINVAL;
    }
    mutex_unlock(&mc->ctl_lock);
    pr_info("modemctl_ioctl() %d\n", ret);
    return ret;
}
Exemplo n.º 3
0
int test_loopback()
{
    int ret;
    pthread_t rx_thread;

    ret = setup_signal_handler();
    if (ret < 0)
        return 1;


    ret = modem_setup();
    if(ret) {
        perror(": Failed to setup modem");
        return ret;
    }

    ret = modem_reset();
    if(ret) {
        perror(": Failed to reset modem");
        return 1;
    }

    ret = modem_start();
    if(ret) {
        perror(": Failed to start modem");
        return 1;
    }

    ret = pthread_create(&rx_thread, NULL, rx_thread_fnc, NULL);
    if(ret) {
        perror(": Error - pthread_create");
        return 1;
    }


    printf("Setup modem defaults\n");
    defaults(INTERNAL_PACKET_GEN, IP_LOOPBACK_MODE);

    if (INTERNAL_PACKET_GEN) {
        sleep(1);
        fill_in_internal_pgen_data();
        printf("Sending 1 packet from internal IP\n");
        reg_write(0x120, 1);
        reg_write(0x120, 0);
        sleep(3);
    } else {
        sleep(1);
        printf("Sending data\n");
        ret = receive_data();
        sleep(3);
    }
    modem_stop();
    pthread_join(rx_thread, NULL);
    modem_close();

    int i;
    bool b = false;
    for(i = 0; i < TX_BUF_SIZE-HEADER_DATA_SIZE; i++) {
        b |= rx_buffer[i] != tx_buffer[i+HEADER_DATA_SIZE];
    }

    if (b) {
        printf("\n---------RX|TX---------\n");
        for(i = 0; i < TX_BUF_SIZE-HEADER_DATA_SIZE; i++) {
            printf("%d %d\n", rx_buffer[i]>>0, tx_buffer[i+HEADER_DATA_SIZE]);
        }
    }

    printf("Exiting test\n");

    return (int) b;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: kLabUM/IoT
void main(){

    CyGlobalIntEnable;

    // Start up initial mote processes
    SleepTimer_Start();
    isr_SleepTimer_StartEx(sleepTimerWake_INT);
    FS_Init();  // SD Card
    modem_set_api_feed(FEED_ID, API_KEY);
    modem_start();
    ultrasonic_start();
    solinst_start();
    ADC_SAR_1_Start();
    //modem_power_off();
    
    packet_ready = 0u; 
    take_reading = 1u;
    t_sample = 2u;                                  // Initialize Sample Period to 2 minutes
    trigger_sampler = 0u;                           // Initialize automated sampler to not take a sample    
    bottle_count = 0u;                              // Initialize bottle count to zero

    NeoRTC_Start(rtcCallBackReceived);              // Start and enable the RTC.   
    NeoRTC_Set_Repeating_Minute_Alarm(t_sample);    // Set 1-minute alarm
   
    
    //  Uncomment below to set RTC
    /*
    NeoRtcTimeStruct tm = {
      .second = 00,
      .minute = 54,
      .hour = 18,
      .day = 27,
      .weekday = 2,
      .month = 5,
      .year = 2014,
    } ;
    RTC_WriteTime(tm); //sets time
    */


    for(;;){
    
        RTC_Process_Tasks();
        
        // Loop continuously and take a reading  
        // every "t_sample" minutes.
        // The variable "take_reading" is set to TRUE 
        // by rtcCallBackReceived() defined after the for() loop
        if(take_reading){ 

            // Turn on the modem to update state variables:
            //  sampling frequency; triggering the autosampler
            modem_power_on();   
           
            if (clear_packet(data_packet)) {
                packet_ready = 0u;
            }
                        
            // To communicate with the IoT platform, the node 
            // 1) listens on a given port and 2) parses an incoming string 
            // for relevant commands. Once a packet is ready to send (3), 
            // the node 4) transmits the packet.  This is achieved in as little 
            // as four lines of code by leveraging an existing TCP/IP library.
            //
            // 1) modem_get_packet()
            // 2) packet_get_uint8()
            // 3) sprintf(data_packet, ...
            // 4) modem_send_packet()
            //
            if (modem_get_packet(data_packet,"t_sample,trigger_sampler")) {
                
                // Read in any updated values
                if(packet_get_uint8(data_packet, "t_sample", &tmp)){
                    t_sample = tmp;
                }
                if(packet_get_uint8(data_packet, "trigger_sampler", &tmp)){
                    trigger_sampler = tmp;
                }
            }

            /*
            if (send_attempts > 0) {
                debug_write("New reading taken before previous packet was sent");
            }
            */

            // Trigger the autosampler to collect the sample 
            // if a sample is requested
            if (trigger_sampler){
                
                // Send in acknowledgement that packet containing
                // info re:automated sampler has been received:
                modem_send_packet("trigger_sampler, 0");
                
                trigger_sampler = 0; // Update the value locally
                
                // Trigger the autosampler as long as there are still available samples
                // If bottle_count >= MAX_BOTTLE_COUNT, skip taking a sample and avoid
                // waiting for the code to timeout
                if (bottle_count < MAX_BOTTLE_COUNT) {
                    // Turn off modem to conserve energy
                    modem_power_off();
                    
                    // Start up the autosampler processes 
                    // and power on the autosampler
                    autosampler_start();
                    autosampler_power_on();
                    

                    // Trigger the autosampler and update current bottle_count
                    autosampler_take_sample(&bottle_count);
                    
                    // Power off the autosampler
                    // and shut down the autosampler processes 
                    autosampler_power_off(); 
                    autosampler_stop();    
                }
                else {
                    debug_write("bottle_count >= MAX_BOTTLE_COUNT");
                }
            }
            
            // Construct the data packet that will be transmitted
            // by the sensor node, starting with the bottle the
            // autosampler last collected a sample in
            // (the packet is in .csv format)
            sprintf(data_packet,"%s, %u\r\n",
                "bottle", bottle_count);

            // Get Sensor Readings for:
            //  Depth, Pressure, Temperature, Conductivity
            // and update the data packet                
            if (solinst_get_reading(&solinst_reading)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "depth_press", solinst_reading.depth);            
                    
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "temp_press", solinst_reading.temp);                      
            }
            
            if (ultrasonic_get_reading(&ultrasonic_reading)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "depth_sonic", ultrasonic_reading.depth);              
            }
            if (ReadBatteryVoltage(&vBattery)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "V_batt", vBattery);              
            }

            // An example of writing data in JSON format
            // This was replaced with .csv format due to 
            // .csv's smaller packet size
            /*
            sprintf(data_packet, "{"
                      "\"method\":\"put\","
                      "\"resource\":\"/feeds/%d\","
                      "\"headers\":{\"X-ApiKey\":\"%s\"},"
                      "\"body\":{\"version\":\"1.0.0\",\"datastreams\":["
                            "{ \"id\" : \"depth_sonic\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"depth_press\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"temp_press\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"trigger_sampler\", \"current_value\" : \"%d\"},"
                            "{ \"id\" : \"V_batt\",  \"current_value\" : \"%f\"}"
                      "]}}",
                      FEED_ID,API_KEY,
                      ultrasonic_reading.depth, solinst_reading.depth, solinst_reading.temp,0u,vBattery);
            */

            // Update flags for sending a packet and taking sensor readings
            packet_ready = 1u;
            take_reading = 0u;
            //send_attempts = 0u;
        
        // Once the flag for packet_ready has been set, 
        // send the packet and save the packet locally
        } else if(packet_ready){
            isr_SleepTimer_Stop;  //  <============= ADDRESS THIS WITH B.K.
            
            // Power on the modem in case it was turned off
            modem_power_on();
            
            // modem_state should now be IDLE or READY
            // and we can send the packet
            modem_send_packet(data_packet);
            
            // Power off the modem and conserve energy
            modem_power_off();
            
            // Backup data to SD Card
            // Use the current time to time-stamp the current reading
            NeoRtcTimeStruct tm_neo = NeoRTC_Read_Time();

            // Overwrite current data_packet with time-stamped data
            sprintf(data_packet, "\r\n%d:%d:%d %d/%d/%d -- [ID %d] u_d=%f s_d=%f s_t=%f bottle=%d v_b=%f", tm_neo.hour, tm_neo.minute,
                             tm_neo.second, tm_neo.day, tm_neo.month, tm_neo.year, moteID,
                             ultrasonic_reading.depth, solinst_reading.depth, solinst_reading.temp,bottle_count,vBattery);
            
            // Write data_packet to a *.txt file on the SD Card
            Write_To_SD_Card("data.txt","a",data_packet,strlen(data_packet));            

            // Clear the current packet in preparation for the next reading
            // when the node awakens from sleep
            if (clear_packet(data_packet)) {
                packet_ready = 0u;
            }
        
        // If either the sensor node should take a reading nor send a packet
        // go to low power mode to increase battery life    
        }else{
            // Set repeating alarm to trigger every "t_sample" minutes
            NeoRTC_Set_Repeating_Minute_Alarm(t_sample);  
            Goto_Low_Power_Mode();
        }
    CyDelay(1u);  // Quick Pause to prevent locking
    }
}