FRDM-K64F Code Share / Mbed 2 deprecated frdm_K64F_Controller

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TransLayer.cpp Source File

TransLayer.cpp

00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 2 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  *
00012  * You should have received a copy of the GNU General Public License
00013  * along with this program; if not, write to the Free Software
00014  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00015  * MA 02110-1301, USA.
00016  *
00017  */
00018 
00019 
00020 
00021 /**
00022 *     \brief           Implements a simple protocol for communication with PC
00023 *     \author          Navin Bhaskar
00024 */
00025 
00026 #include "TransLayer.h"
00027 
00028 
00029 
00030 /**
00031 *     \fn              TransLayer()
00032 *     \brief           constructor for trans layer. Initializes the pointers
00033 *     \param           none
00034 *     \return          none
00035 */
00036 
00037 TransLayer::TransLayer()
00038 {
00039     _head =_tail = NULL;
00040 }
00041 
00042 /**
00043 *     \fn               AddService(call_back_ptr cb, char flag)
00044 *     \brief            Adds a service to the layer. The arguments are passed to
00045 *                       the call back function
00046 *     \param[in]        cb    call back pointer
00047 *     \param[in]        flag  flag to which this packet must respond
00048 *     \return           -1 on error
00049 */
00050 
00051 int TransLayer::AddService(call_back_ptr cb, char flag)
00052 {
00053     service_ptr temp=_head, mid;
00054 
00055     if (NULL == cb) {
00056         return -1;
00057     }
00058 
00059     // we are going to maintain a linked list of services
00060     mid = (service_ptr)malloc(sizeof(service_list));
00061 
00062     if (NULL == mid) {
00063         return -1;
00064     }
00065 
00066     // record entries
00067     mid->service_function = cb;
00068     mid->service_flag = flag;
00069     if (NULL == _head) {
00070         // first entry ever
00071         _head = mid;
00072         mid->next_service = NULL;
00073     } else {
00074         // traverese to end of the list for insertion
00075         while (temp->next_service != NULL) {
00076             temp = temp->next_service;
00077         }
00078         temp->next_service = mid;
00079         mid->next_service = NULL;
00080     }
00081     return 0;
00082 }
00083 
00084 
00085 
00086 /**
00087 *    \fn          LookForService(char flag)
00088 *    \brief       This function looks for a service that services given flag
00089 *    \param[in]    flag   flag of the service
00090 *    \return      service ptr containing that has info on service or NULL if service flag
00091 *                 was not found
00092 */
00093 
00094 service_ptr TransLayer::LookForService(char flag)
00095 {
00096     service_ptr temp = _head;
00097 
00098     while(temp != NULL) {
00099         if(temp->service_flag == flag) {
00100             return temp;
00101         }
00102         temp = temp->next_service;
00103     }
00104 
00105     return NULL;
00106 }
00107 
00108 
00109 /**
00110 *    \fn          MainLoop(void)
00111 *    \brief       This function waits for a flag and then calls appropraite service
00112 *    \param       none
00113 *    \return      none
00114 *    \note        This function never returns has a while(1) at its heart
00115 */
00116 
00117 void TransLayer::MainLoop(Console * cons, PerAccess * per)
00118 {
00119     char temp, cmd=0;
00120     char data_buff[20];
00121     bool start_flag = false, data_start_flag=false, packet_formed = false;
00122     int i =0;
00123 
00124 
00125     while(1) {
00126         if(cons->available() > 0) {
00127             temp = cons->getCh();
00128             if(PACKET_START == temp && data_start_flag == false) {
00129                 // start of packet, siganl the starting of packet
00130                 start_flag = true;
00131                 i=0;
00132             } else if (start_flag == true) {
00133                 cmd = temp;        // this the flag that is going to indicate the service
00134                 start_flag = false;
00135                 data_start_flag = true;
00136             } else if(data_start_flag == true) {
00137 
00138                 if(temp != 0x0d || temp != 0x0a || temp != '\n') {
00139                     data_buff[i] = temp;
00140                     i++;
00141                 }
00142                 if(temp == 0x0d || temp == 0x0a) {
00143                     data_buff[i] = '\0';
00144                     packet_formed = true;
00145                     data_start_flag = false;
00146                 }
00147 
00148             }
00149             if(packet_formed == true) {
00150                 service_ptr ser;
00151                 //SeeServices();
00152                 ser =  LookForService(cmd);
00153                 if(ser != NULL) {
00154                     ser->service_function(cons, per, data_buff, i);
00155                 }
00156                 packet_formed = false;
00157             }
00158         }
00159     }
00160 }
00161 
00162 
00163 
00164 
00165