Exemplo n.º 1
0
Arquivo: sw.cpp Projeto: halcv/voice
void CSw::vSwitchStateMachine()
{
    if (m_pTimer->isWaitSwitch() == false) {
        return;
    }
    

    for (int i = 0;i < SW_NAME_MAX;i++) {
        int iNowState = digitalRead(iGetPort(i));

        switch(m_tSwStatus[i].iOldState) {
        case SW_STATE_OFF:
            if (iNowState == HIGH) {
                vOffToOff(i);
            } else {
                vOffToOn(i);
            }
            break;
        case SW_STATE_ON:
            if (iNowState == HIGH) {
                vOnToOff(i);
            } else {
                vOnToOn(i);
            }
            break;
        case SW_STATE_LONG_ON:
            if (iNowState == HIGH) {
                vLongOnToOff(i);
            } else {
                vLongOnToLongOn(i);
            }
            break;
        }
    }
}
Exemplo n.º 2
0
void *vProcessConnect(void *_vArg)
{
    // Thread's argument is cast to a XPCConnectInfo object
    XPCConnectInfo *connectInfo = (XPCConnectInfo *)_vArg;
    int iRet;                // Number of bytes returned from receiving 
                             // or sending data on the socket
    int iPid;                // Process ID of the spawn application
    char sSocketBuf[1024];            // Stores the raw bytes received from the 
                                      // socket
        
    XPCPort aPort;                // Stores the chosen socket port number

    // XPCClientConnection linked-list pointers
    XPCClientConnection *currentClient;
    XPCClientConnection *previousClient;

    try
    {
        // Receive socket data
        iRet = connectInfo->getSocket()->iRecieveMessage((void *)sSocketBuf, sizeof(sSocketBuf));
        if (iRet == 0)
        {
            cout << "Receive Error: No Data Received of Socket" << endl;
            delete connectInfo;
            return 1;
        }       

        // Cast raw data to a XPCSpawnInfo object
        XPCSpawnInfo *spawnData = (XPCSpawnInfo *)sSocketBuf;

        if (spawnData->cGetAction() == SPAWN)
        {
            // If the action from the connected process is SPAWN, an attempt 
            // is made to spawn the requested application                        
            iPid = fork();
            if (iPid == 0)    // This is the child process
            {
                // The requested application is spawned
                if (execlp(spawnData->sGetAppName(), spawnData->sGetAppName(), NULL) == -1)
                {
                    // If the requested application cannot be 
                    // spawned, a XPCPort object is returned to the 
                    // client with a port value of -1 indicating an 
                    // error occured
                    XPCPort failurePort((long int)-1);
                    iRet = connectInfo->getSocket()->iSendMessage((void *)&failurePort, sizeof(XPCPort));
                    if (iRet == 0)
                    {
                        cerr << "Socket Send Error: Error sending failure to client" << endl;
                    }
                    delete connectInfo;
                    return 1;
                }
            }
        
            // Lock the domain semaphore and insert the socket message into 
            // the XPCClientConnection linked-list
            connectInfo->getSem()->vLockWait();    
            vCClientConnectionInsert(spawnData->sGetAppName(), iPid, connectInfo->getSocket());

            // The semaphore is unlocked once the socket message is inserted 
            // into the linked-list
            connectInfo->getSem()->vUnlock();
        }
        else 
        {
            // Since the connected process's action is REGISTER, the socket 
            // data is cast to a XPCServerSpawn object
            XPCServerSpawn *serverSpawn = (XPCServerSpawn *)sSocketBuf;

            // Retrieve an Unused Port
            int iPort = iGetPort();
            aPort.vSetPort(iPort);

            // The domain sempaphore is locked before the XPCClientConnection 
            // linked-list is traversed
            connectInfo->getSem()->vLockWait();

            // The linked-list is traversed until the registered application 
            // name and process ID can be located
            previousClient = currentClient = firstClient;
            while (currentClient != NULL)
            {
                  if (*currentClient == *serverSpawn) 
                  {    
                          // If the client information is found, the XPCPort 
                          // object is sent to both the client and the 
                          // server
                          iRet = connectInfo->getSocket()->iSendMessage((void *)&aPort, sizeof(XPCPort));
                          if (iRet == 0)
                          {
                              cerr << "Error sending XPCPort to client" << endl;
                          }

                          // A sleep is done to insure the server receives 
                          // the socket port and binds to it before the 
                          // client has the change to connect
                          sleep(2);

                          iRet = currentClient->getSocket()->iSendMessage((void *)&aPort, sizeof(XPCPort));
                          if (iRet == 0)
                          {
                              cerr << "Error sending XPCPort to server" << endl;
                          }

                          // Remove the found client node from the linked-list 
                          vRemoveNode(currentClient);
                          break;
                    }
                    previousClient = currentClient;
                    currentClient = currentClient->next;
                }

                // If the client information could not be located, a XPCPort object 
                // containing a -1 is sent to the server indicating failure
                if (currentClient == NULL)
                {
                    XPCPort failurePort((long int)-1);
                    iRet = connectInfo->getSocket()->iSendMessage((void *)&failurePort, sizeof(XPCPort));
                    if (iRet == 0)
                    {
                        cerr << "Error sending socket data" << endl;
                    }
                 }

                 // The domain semaphore protecting the linked-list is unlocked
                 connectInfo->getSem()->vUnlock();
            }       
        }
        catch(XPCException &exceptObject)
        {
            // All socket related errors are caught and displayed to the user
            cout << "Spawn Server Error: " << exceptObject.sGetException() << endl;
        }
        
        delete connectInfo;
}