mbx.c
Go to the documentation of this file.
00001 #include "mbx.h"
00002 
00003 int openNewMbx(MBX **mbx, const char *name, int buffSize) {
00004         if (!(*mbx = (MBX *) rt_mbx_init(nam2num(name), buffSize+9))) {
00005                 return -1;
00006         }
00007         return 0;
00008 }
00009 
00010 int openNewMbxBuf(MBX **mbx, const char *name, int buffSize, int sizeBuf) {
00011         if (!(*mbx = (MBX *) rt_mbx_init(nam2num(name), (buffSize+9)*sizeBuf))) {
00012                 return -1;
00013         }
00014         return 0;
00015 }
00016 
00017 int openExistingMbx(MBX **mbx, const char *name) {
00018         if (!(*mbx = (MBX *) rt_get_adr(nam2num(name)))) {
00019                 return -1;
00020         }
00021         return 0;
00022 }
00023 
00024 int sendViaMbx(MBX *mbx, void *data, uint32_t sizeIn) {
00025         int i=0;
00026     int count=0;
00027         uint32_t checksum=0;
00028         unsigned char tmp;
00029 
00030         // build checksum
00031         for(i=0; i<sizeIn; i++) {
00032                 checksum = checksum  + ((unsigned char*)data)[i];
00033         }
00034 
00035         tmp = 0xFA;
00036         if ((count = rt_mbx_send_if(mbx, &tmp, 1))) {  //send preeamble
00037                 if (count == 1) {
00038                         //_MY_PRINTLF_S("Mailbox seems to be full");
00039                 } else if (count == -EINVAL) {
00040                         _MY_PRINTLF_S("Pointer to an invalid mailbox");
00041                 } else {
00042                         _MY_PRINTLF_S_I("Weard stuff happens", count);
00043                 }
00044                 return -1;
00045         }
00046 
00047         if ((count = rt_mbx_send_if(mbx, &sizeIn, sizeof(sizeIn)))) {  //send size
00048                 if (count == sizeof(sizeIn)) {
00049                         //_MY_PRINTLF_S("Mailbox seems to be full");
00050                 } else if (count == -EINVAL) {
00051                         _MY_PRINTLF_S("Pointer to an invalid mailbox");
00052                 } else {
00053                         _MY_PRINTLF_S_I("Weard stuff happens", count);
00054                 }
00055                 return -2;
00056         }
00057 
00058         if ((count = rt_mbx_send_if(mbx, &checksum, sizeof(checksum)))) {  //send checksum
00059                 if (count == sizeof(checksum)) {
00060                         //_MY_PRINTLF_S("Mailbox seems to be full");
00061                 } else if (count == -EINVAL) {
00062                         _MY_PRINTLF_S("Pointer to an invalid mailbox");
00063                 } else {
00064                         _MY_PRINTLF_S_I("Weard stuff happens", count);
00065                 }
00066                 return -3;
00067         }
00068 
00069         if ((count = rt_mbx_send_if(mbx, data, sizeIn))) {  //send data
00070                 if (count == sizeIn) {
00071                         //_MY_PRINTLF_S("Mailbox seems to be full");
00072                 } else if (count == -EINVAL) {
00073                         _MY_PRINTLF_S("Pointer to an invalid mailbox");
00074                 } else {
00075                         _MY_PRINTLF_S_I("Weard stuff happens", count);
00076                 }
00077                 return -4;
00078         }
00079 
00080         return 0;
00081 }
00082 
00083 
00084 int receiveViaMbx(MBX *mbx, void *data, uint32_t sizeMax) {
00085         int count=0, i=0;
00086     uint32_t size=0, checkSum=0;
00087 
00088     unsigned char tmp = 0;
00089     do {
00090         if ((count = rt_mbx_receive_if(mbx, (void *) &tmp, 1)) < 0) {
00091                         if (count == -EINVAL) {
00092                                 _MY_PRINTLF_S("Pointer to an invalid mailbox");
00093                         } else {
00094                 _MY_PRINTLF_S_I("Receiving Error ", count);
00095                         }
00096             return -1;
00097         }
00098         if (count != 0) { return 0;} /*MBX empty*/
00099     } while (tmp != 0xfa );
00100         /* the starting flag of new data was received */
00101 
00102         if ((count = rt_mbx_receive(mbx, &size, sizeof(uint32_t)))) {
00103                 _MY_PRINTLF_S("Receiving Error, seems there is no data.");
00104                 return -2;
00105     }
00106     size = (sizeMax < size) ? sizeMax : size;
00107 
00108         if ((count = rt_mbx_receive(mbx, &checkSum, sizeof(uint32_t)))) {
00109                 _MY_PRINTLF_S("Receiving Error, seems there is no data.");
00110                 return -3;
00111     }
00112 
00113         if (count == 0 && size > 0){
00114                 if ((count = rt_mbx_receive(mbx, data, size))) {
00115                         _MY_PRINTLF_S("Receiving Error, seems there is no data.");
00116                         return -4;
00117                 }
00118 
00119                 //evaluate checksum
00120                 for(i=0; i<size; i++) {
00121                         checkSum = checkSum  - ((unsigned char*)data)[i];
00122                 }
00123         } else {
00124                 return -10; /* just for safty, should not be reached */
00125         }
00126 
00127         if (checkSum != 0) {
00128                 size = 0;
00129                 return -99;
00130         }
00131 
00132         return size;
00133 }
00134 
00135 int closeMbx(MBX *mbx) {
00136         int status = rt_mbx_delete(mbx);
00137         if (status == -EINVAL) {
00138                 _MY_PRINTLF_S("Pointer to an invalid mailbox");
00139         } else if (status == -EFAULT) {
00140                 _MY_PRINTLF_S("Mailbox data were found in an invalid state");
00141         }
00142 
00143         return status;
00144 }


rtai_wrapper
Author(s): Johannes Mayr
autogenerated on Mon Jan 6 2014 11:08:05