void Handle::setCategoryData(const char *category, void *user_data) { Error error; if (!LSCategorySetData(_handle, category, user_data, error.get())) { throw error; } }
int main(void) { GMainLoop *mainLoop = g_main_loop_new(NULL, FALSE); void *userData = mainLoop; g_timeout_add(10000, &OnTimeout, mainLoop); GThread *client_thread = g_thread_new(NULL, ClientProc, mainLoop); /*! [service registration] */ bool retVal; LSError lserror; LSErrorInit(&lserror); LSHandle *serviceHandle = NULL; retVal = LSRegister("com.palm.contacts", &serviceHandle, &lserror); if (!retVal) goto error; retVal = LSRegisterCategory(serviceHandle, "/category", ipcMethods, NULL, NULL, &lserror); if (!retVal) goto error; retVal = LSCategorySetData(serviceHandle, "/category", userData, &lserror); if (!retVal) goto error; retVal = LSGmainAttach(serviceHandle, mainLoop, &lserror); if (!retVal) goto error; g_main_loop_run(mainLoop); /*! [service registration] */ LSUnregister(serviceHandle, &lserror); g_main_loop_unref(mainLoop); g_thread_join(client_thread); if (hit_reply) { printf("PASS\n"); return 0; } printf("FAILED\n"); return 1; error: LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); if (serviceHandle) LSUnregister(serviceHandle, &lserror); g_main_loop_unref(mainLoop); g_thread_join(client_thread); fprintf(stderr, "FAILED\n"); return 1; }
MojErr MojLunaService::addCategory(const MojChar* name, CategoryHandler* handler) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(name && handler); MojAssertMutexUnlocked(m_mutex); MojThreadGuard guard(m_mutex); // create array of LSMethods MethodVec pubMethods; MethodVec privMethods; const CategoryHandler::CallbackMap& callbacks = handler->callbacks(); for (CategoryHandler::CallbackMap::ConstIterator i = callbacks.begin(); i != callbacks.end(); i++) { LSMethod m = {i.key(), &handleRequest}; MethodVec& methods = i->m_pub ? pubMethods : privMethods; MojErr err = methods.push(m); MojErrCheck(err); } LSMethod nullMethod = {NULL, NULL}; MojErr err = pubMethods.push(nullMethod); MojErrCheck(err); err = privMethods.push(nullMethod); MojErrCheck(err); // create category object to hang on to method array MojRefCountedPtr<LunaCategory> cat(new LunaCategory(this, handler, pubMethods, privMethods)); MojAllocCheck(cat.get()); LSMethod* pubLsMethods = const_cast<LSMethod*>(cat->m_pubMethods.begin()); LSMethod* privLsMethods = const_cast<LSMethod*>(cat->m_privMethods.begin()); MojLunaErr lserr; bool retVal; if (m_service) { retVal = LSPalmServiceRegisterCategory(m_service, name, pubLsMethods, privLsMethods, NULL, cat.get(), lserr); MojLsErrCheck(retVal, lserr); } else { MojAssert(m_handle); retVal = LSRegisterCategory(m_handle, name, privLsMethods, NULL, NULL, lserr); MojLsErrCheck(retVal, lserr); retVal = LSCategorySetData(m_handle, name, cat.get(), lserr); MojLsErrCheck(retVal, lserr); } MojString categoryStr; err = categoryStr.assign(name); MojErrCheck(err); err = m_categories.put(categoryStr, cat); MojErrCheck(err); return MojErrNone; }
struct property_service* property_service_create() { struct property_service *service; LSError error; service = g_try_new0(struct property_service, 1); if (!service) return NULL; LSErrorInit(&error); if (!LSRegisterPubPriv("com.android.properties", &service->handle, false, &error)) { g_error("Failed to register the luna service: %s", error.message); LSErrorFree(&error); goto error; } if (!LSRegisterCategory(service->handle, "/", property_service_methods, NULL, NULL, &error)) { g_error("Could not register service category: %s", error.message); LSErrorFree(&error); goto error; } if (!LSCategorySetData(service->handle, "/", service, &error)) { g_error("Could not set daa for service category: %s", error.message); LSErrorFree(&error); goto error; } if (!LSGmainAttach(service->handle, event_loop, &error)) { g_error("Could not attach service handle to mainloop: %s", error.message); LSErrorFree(&error); goto error; } return service; error: if (service->handle != NULL) { LSUnregister(service->handle, &error); LSErrorFree(&error); } g_free(service); return NULL; }
AmbientLightSensor::AmbientLightSensor () : m_service(NULL) , m_alsEnabled(false) , m_alsIsOn(false) , m_alsPointer(0) , m_alsRegion(ALS_REGION_UNDEFINED) , m_alsSum(0) , m_alsLastOff(0) , m_alsDisplayOn(false) , m_alsSubscriptions(0) , m_alsDisabled(0) , m_alsHiddOnline(false) , m_alsFastRate(false) , m_alsSampleCount(0) , m_alsCountInRegion(0) , m_alsSamplesNeeded (ALS_INIT_SAMPLE_SIZE) , m_alsLastSampleTs (0) { LSError lserror; LSErrorInit(&lserror); bool result; GMainLoop* mainLoop = HostBase::instance()->mainLoop(); result = LSRegister(AMBIENT_LIGHT_SENSOR_ID, &m_service, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree(&lserror); } result = LSRegisterCategory (m_service, "/control", alsMethods, NULL, NULL, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree(&lserror); } result = LSCategorySetData (m_service, "/control", this, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree(&lserror); } result = LSSubscriptionSetCancelFunction(m_service, AmbientLightSensor::cancelSubscription, this, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } result = LSGmainAttach(m_service, mainLoop, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree(&lserror); } result = LSRegisterServerStatus(m_service, "com.palm.hidd", AmbientLightSensor::hiddServiceNotification, this, &lserror); if (!result) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } #if defined (MACHINE_CASTLE) m_alsEnabled = true; m_alsBorder[0] = 10 * ALS_SAMPLE_SIZE; m_alsBorder[1] = 200 * ALS_SAMPLE_SIZE; m_alsMargin[0] = 2 * ALS_SAMPLE_SIZE; m_alsMargin[1] = (m_alsBorder[1] / 10) > (ALS_SAMPLE_SIZE * 2) ? m_alsBorder[1] / 10 : ALS_SAMPLE_SIZE * 2; #else if (Settings::LunaSettings()->enableAls) { m_alsEnabled = true; g_warning ("ALSCal token found, expecting lux values in light events"); m_alsBorder[ALS_REGION_UNDEFINED] = -1; m_alsBorder[ALS_REGION_DARK] = 6; m_alsBorder[ALS_REGION_DIM] = 100; m_alsBorder[ALS_REGION_INDOOR] = 1000; m_alsBorder[ALS_REGION_OUTDOOR] = INT_MAX; // margins are higher at lower lux values m_alsMargin[ALS_REGION_UNDEFINED] = 0; m_alsMargin[ALS_REGION_DARK] = 4; m_alsMargin[ALS_REGION_DIM] = 10; m_alsMargin[ALS_REGION_INDOOR] = 100; m_alsMargin[ALS_REGION_OUTDOOR] = 0; } else { g_warning ("%s: ALS is not enabled", __PRETTY_FUNCTION__); } #endif m_instance = this; g_debug ("%s started", __PRETTY_FUNCTION__); }
struct audio_service* audio_service_create() { struct audio_service *service; LSError error; pa_mainloop_api *mainloop_api; char name[100]; service = g_try_new0(struct audio_service, 1); if (!service) return NULL; LSErrorInit(&error); if (!LSRegisterPubPriv("org.webosports.audio", &service->handle, false, &error)) { g_warning("Failed to register the luna service: %s", error.message); LSErrorFree(&error); goto error; } if (!LSRegisterCategory(service->handle, "/", audio_service_methods, NULL, NULL, &error)) { g_warning("Could not register service category: %s", error.message); LSErrorFree(&error); goto error; } if (!LSCategorySetData(service->handle, "/", service, &error)) { g_warning("Could not set daa for service category: %s", error.message); LSErrorFree(&error); goto error; } if (!LSGmainAttach(service->handle, event_loop, &error)) { g_warning("Could not attach service handle to mainloop: %s", error.message); LSErrorFree(&error); goto error; } service->pa_mainloop = pa_glib_mainloop_new(g_main_context_default()); mainloop_api = pa_glib_mainloop_get_api(service->pa_mainloop); snprintf(name, 100, "AudioServiceContext:%i", getpid()); service->context = pa_context_new(mainloop_api, name); service->context_initialized = false; pa_context_set_state_callback(service->context, context_state_cb, service); if (pa_context_connect(service->context, NULL, 0, NULL) < 0) { g_warning("Failed to connect to PulseAudio"); pa_context_unref(service->context); pa_glib_mainloop_free(service->pa_mainloop); goto error; } sample_list = g_slist_alloc(); return service; error: if (service->handle != NULL) { LSUnregister(service->handle, &error); LSErrorFree(&error); } g_free(service); return NULL; }