Пример #1
0
/**
* @brief Post a notification to all subscribers with name 'key'.
*
* This is equivalent to:
* LSSubscriptionReply(public_bus, ...)
* LSSubscriptionReply(private_bus, ...)
*
* @param  psh
* @param  key
* @param  payload
* @param  lserror
*
* @retval
*/
bool
LSSubscriptionRespond(LSPalmService *psh, const char *key,
                      const char *payload, LSError *lserror)
{
    LSHandle *public_bus = LSPalmServiceGetPublicConnection(psh);
    LSHandle *private_bus = LSPalmServiceGetPrivateConnection(psh);
    bool retVal;

    retVal = LSSubscriptionReply(public_bus, key, payload, lserror);
    if (!retVal) return retVal;

    retVal = LSSubscriptionReply(private_bus, key, payload, lserror);
    if (!retVal) return retVal;

    return true;
}
Пример #2
0
/**
* @brief Posts a message to all in subscription '/category/method'.
*        This is equivalent to:
*        LSSubscriptionReply(sh, '/category/method', payload, lserror)
*
* @deprecated Please use LSSubscriptionReply() instead.
*
* @param  sh
* @param  category
* @param  method
* @param  payload
* @param  lserror
*
* @retval
*/
bool
LSSubscriptionPost(LSHandle *sh, const char *category,
                   const char *method,
                   const char *payload, LSError *lserror)
{
    LSHANDLE_VALIDATE(sh);

    bool retVal = false;
    char *key = _LSMessageGetKindHelper(category, method);

    retVal = LSSubscriptionReply(sh, key, payload, lserror);

    g_free(key);
    return retVal;
}
bool AmbientLightSensor::updateAls(int intensity)
{
#if defined (TARGET_DEVICE)
    LSError lserror;
    LSErrorInit(&lserror);
    bool result = true;

    int current = m_alsRegion;

    if (m_alsDisabled > 0) {
        m_alsRegion = ALS_REGION_UNDEFINED;
        g_debug(
                "%s: reported light level of %d [region set to default by subscription]",
                __PRETTY_FUNCTION__, intensity);

        goto end;
    }

    if (!m_alsEnabled) {
        m_alsRegion = ALS_REGION_UNDEFINED;
        g_debug("%s: reported light level of %d [device not calibrated]",
                __PRETTY_FUNCTION__, intensity);

        goto end;
    }

    if (intensity < 0) {
        g_warning("%s: invalid intensity %d", __PRETTY_FUNCTION__, intensity);
        return false;
    }

    if (m_alsRegion < ALS_REGION_UNDEFINED || m_alsRegion > ALS_REGION_OUTDOOR) {
        g_warning("%s: current region is invalid, resetting to indoor",
                __PRETTY_FUNCTION__);
        m_alsRegion = ALS_REGION_INDOOR;
    }

    if (m_alsSampleCount == 0)
    {
        m_alsSum = 0;
    }

    if (m_alsSampleCount == m_alsSamplesNeeded) {
        m_alsSum -= m_alsSampleList.front();
        m_alsSampleList.pop_front();
        m_alsSampleCount--;
    }

    // start collecting samples
    if (m_alsSampleCount < m_alsSamplesNeeded) {
        // g_debug("%s: received sample %d", __PRETTY_FUNCTION__, intensity);
        // maintaining a running sum of the last m_alsSamplesNeeded number of samples
        // also maintaining the last m_alsSamplesNeeded values in an array
        m_alsSampleList.push_back(intensity);
        m_alsSum += intensity;
        m_alsSampleCount++;
    }

    if (intensity < m_alsBorder[m_alsRegion - 1] - m_alsMargin[m_alsRegion - 1] || intensity > m_alsBorder[m_alsRegion] + m_alsMargin[m_alsRegion])
    {
        if (!m_alsFastRate)
        {
            m_alsCountInRegion = 0;
            g_debug ("resetting ALS to sample at fast rate");
            // switch to the slow mode
            InputControl* ic = HostBase::instance()->getInputControlALS();
            if (NULL != ic)
            {
                if (!ic->setRate(NYX_REPORT_RATE_HIGH))
                    return false;
            }
            m_alsFastRate = true;
        }
    } else {
        if (m_alsCountInRegion == ALS_INIT_SAMPLE_SIZE)
        {
            if (m_alsFastRate) {
                g_debug ("resetting ALS to sample at slow rate");
                // switch to the slow mode
                InputControl* ic = HostBase::instance()->getInputControlALS();
                if (NULL != ic)
                {
                    if (!ic->setRate(NYX_REPORT_RATE_LOW))
                        return false;
                }
                m_alsFastRate = false;
            }
        }
        if (m_alsCountInRegion <= ALS_INIT_SAMPLE_SIZE)
            m_alsCountInRegion++;
    }

    if (m_alsSampleCount == m_alsSamplesNeeded) {
        // sample count is now the required sample size, estimate ALS region
        while (m_alsRegion > ALS_REGION_DARK && (m_alsSum / ALS_SAMPLE_SIZE)
                < (m_alsBorder[m_alsRegion - 1] - m_alsMargin[m_alsRegion - 1])) {
            --m_alsRegion;
        }

        while (m_alsRegion < ALS_REGION_OUTDOOR && (m_alsSum / ALS_SAMPLE_SIZE)
                > (m_alsBorder[m_alsRegion] + m_alsMargin[m_alsRegion])) {
            ++m_alsRegion;
        }
    }

end:

    if (m_alsSubscriptions > 0) {
        gchar *status = g_strdup_printf(
                "{\"returnValue\":true,\"current\":%i,\"region\":%i}",
                intensity, m_alsRegion);

        if (NULL != status)
            result = LSSubscriptionReply(m_service, "/control/status", status,
                    &lserror);
        if (!result) {
            LSErrorPrint(&lserror, stderr);
            LSErrorFree(&lserror);
        }
        g_free(status);
    }

    // if there was no change return false, no need to update anything
    return (m_alsRegion != current);
#else
    return false;
#endif
}
// this function is called only on castle due to the sensor limitations
// the als region is calculated from a running average of the als values
// this causes the als region change to occur slowly
bool AmbientLightSensor::updateAlsCastle (int intensity)
{
#if defined (TARGET_DEVICE)
    LSError lserror;
    LSErrorInit(&lserror);
    bool result = true;

    int current = m_alsRegion;
    int oldPtr = m_alsPointer;

    if (m_alsSampleCount == 0)
    {
        // the first sample comes is, assume others will be close
        int i = 0;
        for ( ; i < m_alsSamplesNeeded ; i++)
            m_alsValue[i] = intensity;
        m_alsSum = intensity * m_alsSamplesNeeded;
    }
    else
    {
        m_alsSum -= m_alsValue[m_alsPointer];
        m_alsValue[m_alsPointer] = intensity;
        m_alsSum += intensity;
    }

    if (m_alsSampleCount < m_alsSamplesNeeded)
    {
        m_alsSampleCount++;
        if (m_alsSampleCount == m_alsSamplesNeeded)
        {
            g_debug ("resetting ALS to sample at 1sec");

            // switch to the slow mode
            InputControl* ic = HostBase::instance()->getInputControlALS();
            if (NULL != ic)
            {
                if (!ic->setRate(NYX_REPORT_RATE_LOW))
                    return false;
            }
        }
    }

    m_alsPointer = (m_alsPointer + 1) % m_alsSamplesNeeded;

    if (m_alsDisabled > 0)
    {
        m_alsRegion = ALS_REGION_UNDEFINED;
        g_debug ("%s: reported light level of %i (%i) [region set to default by subscription]", __PRETTY_FUNCTION__, intensity, m_alsSum / m_alsSamplesNeeded);

        goto end;
    }

    if (!m_alsEnabled)
    {
        m_alsRegion = ALS_REGION_UNDEFINED;
        g_debug ("%s: reported light level of %i (%i) [device not calibrated]", __PRETTY_FUNCTION__, intensity, m_alsSum / m_alsSamplesNeeded);

        goto end;
    }

    if (m_alsSum < m_alsBorder[0])
    {
        if (current == ALS_REGION_INDOOR && m_alsSum >= m_alsBorder[0] - m_alsMargin[0])
            m_alsRegion = ALS_REGION_INDOOR;
        else
            m_alsRegion = ALS_REGION_DARK;
    }
    else if (m_alsSum < m_alsBorder[1])
    {
        if (current == ALS_REGION_DARK && m_alsSum < m_alsBorder[0] + m_alsMargin[0])
            m_alsRegion = ALS_REGION_DARK;
        else if (current == ALS_REGION_OUTDOOR && m_alsSum >= m_alsBorder[1] - m_alsMargin[1])
            m_alsRegion = ALS_REGION_OUTDOOR;
        else
            m_alsRegion = ALS_REGION_INDOOR;
    }
    else
    {
        if (current == ALS_REGION_INDOOR && m_alsSum < m_alsBorder[1] + m_alsMargin[1])
            m_alsRegion = ALS_REGION_INDOOR;
        else
            m_alsRegion = ALS_REGION_OUTDOOR;
    }

end:

    if (m_alsSubscriptions > 0)
    {
        gchar *status = g_strdup_printf ("{\"returnValue\":true,\"current\":%i,\"average\":%i,\"region\":%i}",
                m_alsValue[oldPtr], m_alsSum / m_alsSamplesNeeded, m_alsRegion);

        if (NULL != status)
            result = LSSubscriptionReply(m_service, "/control/status", status, &lserror);
        if(!result)
        {
            LSErrorPrint (&lserror, stderr);
            LSErrorFree (&lserror);
        }
        g_free(status);
    }

    // if there was no change return false, no need to update anything
    return  (m_alsRegion != current);
#else
    return false;
#endif
}