Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Sample Code

The following sample code creates an instance of IPort1 for class AEECLSID_Port1Sensor. Sensors applications will need to create the instance of IPort1 for class ID AEECLSID_Port1Sensor in order to configure and communicate with the hardware.


         
/* Start function */
         pISensor = NULL;
         
         /* Create IPort1 Object */
         if (nErr = IEnv_CreateInstance(pIEnv, AEECLSID_Port1Sensor , 
                        (void**) (&pISensor)) != SUCCESS)
         {
            pISensor = NULL;           
            return FALSE;
         }
         
         //Create the ISensorUtil Object for class ID 
         //AEECLSID_SensorUtil
         pISensorUtil = NULL;   
         /* Create SensorPort Object */
         if (nErr = IEnv_CreateInstance(pIEnv, AEECLSID_SensorUtil , 
                     (void**) (&pISensorUtil)) != SUCCESS)
         {
            pISensor = NULL;
            return FALSE;
         }
         
         
         //Register with SensorUtil and Request auto-generation 
         //of sensor data
         ISignalCBFactory    *pGetData_SignalCBFactory;
         ISignal             *pGetData_Signal;
         ISignalCtrl         *pGetData_SignalCtrl;
         
         ISignalCBFactory    *writeData_SignalCBFactory;
         ISignal             *writeData_Signal;
         ISignalCtrl         *writeData_SignalCtl;
         
         IEnv_CreateInstance(pIEnv, AEECLSID_SignalCBFactory 
                                 (void**)&pGetData_SignalCBFactory);
         
         ISignalCBFactory_CreateSignal(pGetData_SignalCBFactory, 
                  Sensor_Get_Data, (void*)me, 
                  pGetData_Signal, pGetData_SignalCtrl);
         
         ....
         ....
         
         /* Call IPort1_Readable to receive callbacks */
         IPort1_Readable(pISensor, pGetData_Signal );
         
         WriteData(pMe);
         
         ....
         ....
/* End function */
         /////////////////////////////////////////////////
         /////////////////////////////////////////////////
void WriteData(void* pUserData)
{ 
   int   ret = AEE_SUCCESS;
   int    i;
   App* pMe = (App*)pUserData;
         
   Ret = IPort1_Write(pISensor, (byte*)sendBuf, 
            STRLEN(sendbuf)+1, &i));
         
   if(ret == AEE_EWOULDBLOCK)
   {
      IEnv_CreateInstance( pIEnv, AEECLSID_SignalCBFactory, 
                              (void**)&writeData_SignalCBFactory );
      ISignalCBFactory_CreateSignal( writeData_SignalCBFactory, 
               WriteData, pMe, &writeData_Signal, 
               &writeData_SignalCtl );
         
      IPort1_Writeable(pISensor, writeData_Signal ); 
   }
   if(ret == AEE_EFAILED)
   {
      // write failed
    }
    return;
}
         
         /////////////////////////////////////////////////
         /////////////////////////////////////////////////
Sensor_GetData(void *pUserData)
{
   App *pMe, (App*)po;
   int   ret = AEE_SUCCESS;
   int    i;
   ISensorUtil_iterator iter = 0;
   uint32 numKeys, numValues, numResponses;
   char requestStr[100], nextKey[32], nextAttr[32], sys[100];
   int pResStrLen, nextKeyLen, nextAttrLen, result, res;
   int64 nextValue, nValue, ts;  
   int32 error;
   boolean bIsValid;
         
   nret = IPort1_Read(pISensor, (byte *)receiveBuf, 
                        MAX_SIZE_RECEIVE_BUFFER, &i);
   if(nret == AEE_EWOULDBLOCK)
   {
      //No Data available yet...schedule callback
      IPort1_Readable( pISensor, pGetData_Signal );
      ISignalCtl_Enable(pGetData_SignalCtrl);
      return;
   }
   else
   {
      //Response Received in receivefreqbuf
         
      //Create the isensorutil iterator
      result = ISensorUtil_IterCreate( pISensorUtil, receivebuf, 
                           STRLEN(receivebuf)+1, &iter );
      if( AEE_SUCCESS == result )
      {
         ISensorUtil_IterNumMessagesInResponse( pISensorUtil, iter, 
                                    &numResponses);
         //Number of Responses is  numResponses
         
         while( numResponses != 0 )
         {
            // Get error code from response
            result = ISensorUtil_IterNextErrorCode( pISensorUtil, 
                           iter, &error );
            if ( AEE_SUCCESS == result )
            {
               if(error != 0)
               {
                  //Response contained Error
                  goto label;
               }
            }
            else
            {
               //ISensorUtil_GetErrorInResponse Failed
            }       
         //Get the request string in the response
         result = ISensorUtil_IterNextRequestString( pISensorUtil, 
                                 iter, requestStr, 100, &pResStrLen );
         if( AEE_SUCCESS != result )
         {
            //Invalid String
            goto label;
         }
         //Request Strings is requestStr
         numResponses --;
      }
         
      //Iter number of Keys
      result = ISensorUtil_IterNumKeys( pISensorUtil, iter, &numKeys );
      if( AEE_SUCCESS == result )
      {
         //Number of keys is numKeys
         while( numKeys != 0 )
         {
            //Iter the next Key
            result = ISensorUtil_IterNextKey( pISensorUtil, iter, 
                        nextKey, 32, &nextKeyLen, nextAttr, 
                        32, &nextAttrLen );
            if( AEE_SUCCESS == result )
            {
               if( NULL != nextKey )
               {
                  //DataType:nextKey
         
                  //Iter the number of values
                  result = ISensorUtil_IterNumValues(pISensorUtil, 
                              iter, nextKey, sizeof(nextKey), 
                              nextAttr, sizeof(nextKey), &numValues);
                  if( AEE_SUCCESS == result )
                  {
                     while( numValues != 0 )
                     {
                        //Number of values: numValues
                        //Iter the next value
                        result = ISensorUtil_IterNextValue( pISensorUtil, 
                                 iter, &nextValue, &ts, sys, 100, 
                                 &res, &bIsValid );
                        if( AEE_SUCCESS == result )
                        {
                           //bIsValid is bIsValid
                           if( TRUE == bIsValid )
                           {
                              if( !STRCMP(sys, "") )
                              {
                                 //Value:nextValue
                                 //TimeStamp:ts
                              }
                              else
                              {
                                 //system attribute or string attribute 
                                 //value
                              }
                           }
                        }
                        else
                        {
                           //ISensorUtil_IterNextValue Failed
                        }
                        numValues--;
                     }
                  }
                  else
                  {
                     //ISensorUtil_IterNumValues Failed
                  }
               }                 
            }
            else
            {
               //ISensorUtil_IterNextKey Failed
            }
            numKeys--;
         }
      }
      else
      {
         //ISensorUtil_IterNumKeys Failed
      }
   }
   else
   {
      //ISensorUtil_IterCreate Failed
   }
  label:        
    //Delete the iterator
    result = ISensorUtil_IterDelete( pISensorUtil, iter );
    if( AEE_SUCCESS != result )
    {
      //ISensorUtil_IterDelete Failed
    } 
  }
  // Call ISignalCtl_Enable to receive more callbacks
  ISignalCtl_Enable(pMe->getData_SignalCtl);
  return;
}
      
      

The following code shows sample return values.

         
         {
         // Assume pResponseStr has the message 
         //"error=0#sys=accelx;accely;accelz;pressure;step#get,sys"   
         
         // myIter is the iterator having the pResponseStr
         ISensorUtil_iterator myIter;
         
         int64 nValue;
         boolean bIsValid;
         char pDataType[32];
         char pSys[32];
         uint64 ts;
         
         {
         ISensorUtil_IterNextKey(me, myIter, pDataType, 
                  STRLEN(pDataType)+1, &pDataTypeLenReq, NULL, 
                  STRLEN(pAttribute)+1, &pAttributeLenReq); 
         
         // pDataType returns "sys" first time
         
         ISensorUtil_IterNextValue(me, myIter, &nValue, ts, pSys, 
                  &bIsValid);        
         }
         
         // nValue return 0 first time
         // bIsValid returns 1
         // ts returns 0 first time
         //sys returns "accelx" first time
         
         // nValue return 0 second time
         // bIsValid returns 1
         // ts returns 0 second time
         //sys returns "accely" second time
         
         // nValue return 176 third time
         // bIsValid returns 1
         // ts returns 0 third time
         //sys returns "accelz" third time
         
         // nValue return 176 fourth time
         // bIsValid returns 1
         // ts returns 0 fourth time
         //sys returns "pressure" fourth time
         
         // nValue return 176 fifth time
         // bIsValid returns 1
         // ts returns 0 fifth time
         //sys returns "step" fifth time
         }
         
      

More sample code for each of the ISensorUtil interfaces is available in the Brew MP SDK API Reference.