blob: 6529ab2c399929c0614614061b4dbcbfb8363eb6 [file] [log] [blame]
/*
* Copyright (C) 2014 MediaTek Inc.
*
* Modification based on code covered by the below mentioned copyright
* and/or permission notice(s).
*/
/*
**
** Copyright 2006 The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <vendor-ril/telephony/ril.h>
#include <log/log.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <pthread.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "common.h"
#include "lynq_common.h"
#include "pms/lynq_pms.h"
#include "network.h"
#define CHECK_TIME 5 //check whether online
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}\
while (0);\
extern "C" void RIL_register (const RIL_RadioFunctions *callbacks);
extern "C" void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
void *response, size_t responselen);
#if defined(ANDROID_MULTI_SIM)
extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
size_t datalen, RIL_SOCKET_ID socket_id);
#else
extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
size_t datalen);
#endif
#undef LOG_TAG
#define LOG_TAG "DEMO_MAIN"
#define UEVENT_BUFFER_SIZE 2048
static struct RIL_Env s_rilEnv = {
RIL_onRequestComplete,
RIL_onUnsolicitedResponse,
NULL
};
void creat_daemon(void)
{
pid_t pid;
pid = fork();
if( pid == -1)
ERR_EXIT("fork error");
if(pid > 0 )
exit(EXIT_SUCCESS);
if(setsid() == -1)
ERR_EXIT("SETSID ERROR");
chdir("/");
int i;
for( i = 0; i < 3; ++i)
{
close(i);
open("/dev/null", O_RDWR);
dup(0);
dup(0);
}
umask(0);
return;
}
extern int sleep_flag;
extern pms_manager * g_pmsManagement;
void signal_handler_fun(int signum) {
RLOGD("signal_handler_fun catch signal %d\n", signum);
//printf("signal_handler_fun catch signal %d\n", signum);
check_autosleep(signum);
//RLOGD("signal_handler_fun catch signal %d %d\n", signum, __LINE__);
//printf("signal_handler_fun catch signal %d %d\n", signum, __LINE__);
return;
}
static void query_network_state(int request, int slot)
{
RequestInfo* info = creatRILInfoAndInit(request, INIT, (RIL_SOCKET_ID) ((slot)));
switch (request)
{
case RIL_REQUEST_DATA_REGISTRATION_STATE:
/* code */
getDataRegistrationState(1, NULL, (RIL_SOCKET_ID) (slot), info);
break;
case RIL_REQUEST_VOICE_REGISTRATION_STATE:
/* code */
getVoiceRegistrationState(1, NULL, (RIL_SOCKET_ID) (slot), info);
break;
default:
break;
}
return;
}
/**
* @brief 1 means can go to autosleep
*
* @return int
*/
static int lynq_ensure_online(void)
{
#if 0
/*ensure inject 2G/3G/4G*/
int slot_id = get_default_sim_all();
int i = 3;
while (i)
{
if(!sleep_flag)
{
RLOGD("function %s no network\n", __FUNCTION__);
printf("%s no network\n", __FUNCTION__);
}
else
{
RLOGD("%s network is online\n", __FUNCTION__);
printf("%s network is online\n",__FUNCTION__);
break;
}
//query_network_state(RIL_REQUEST_DATA_REGISTRATION_STATE, slot_id);
query_network_state(RIL_REQUEST_VOICE_REGISTRATION_STATE, slot_id);
sleep(1);
i--;
}
printf("%s i %d\n",__FUNCTION__, i);
#endif
return 1;
}
/**
* @brief Get the prop
*
* @param ptr
*/
static char* get_pro(const char *ptr)
{
FILE *fp;
static char res[128] = {};
bzero(res, 128);
fp = popen(ptr,"r");
fgets(res,sizeof(res),fp);
printf("buffer %s",res);
pclose(fp);
parse_version_buf(res);
return res;
}
void lynq_goto_sleep(void)
{
if(lynq_ensure_online())//ensure online
{
if(g_lynq_mwakeupcfg != 0)//means user must be configed it
{
RLOGD("means user must be configed it go to sleep\n");
//printf("means user must be configed it go to sleep\n");
sleep(5);//for too early to send command cause fail
auto_suspend(g_lynq_mpwrsm);
//sleep(4);
}
}
else
{
RLOGD("can't autosleep because network is unreable %d\n", sleep_flag);
//printf("can't autosleep because network is unreable %d\n", sleep_flag);
}
sleep_end = 1;
return;
}
/*check usb connect/disconnect*/
void * hot_usb(void * arg)
{
struct sockaddr_nl client;
struct timeval tv;
int CppLive, rcvlen, ret;
fd_set fds;
int buffersize = 1024;
CppLive = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
memset(&client, 0, sizeof(client));
client.nl_family = AF_NETLINK;
client.nl_pid = getpid();
client.nl_groups = 1; /* receive broadcast message*/
setsockopt(CppLive, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
bind(CppLive, (struct sockaddr*)&client, sizeof(client));
while (1)
{
char buf[UEVENT_BUFFER_SIZE] = { 0 };
FD_ZERO(&fds);
FD_SET(CppLive, &fds);
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000;
ret = select(CppLive + 1, &fds, NULL, NULL, &tv);
if(ret < 0)
continue;
if(!(ret > 0 && FD_ISSET(CppLive, &fds)))
continue;
/* receive data */
rcvlen = recv(CppLive, &buf, sizeof(buf), 0);
if (rcvlen > 0)
{
check_autosleep(777);
/*You can do something here to make the program more perfect!!!*/
}
}
close(CppLive);
return NULL;
}
int main(int argc, char **argv) {
system("route add -host 255.255.255.255 dev lo");
signal(SIGHUP,signal_handler_fun);
/*lei add for hot usb plug in 2022/8/27*/
pthread_t thid;
pthread_attr_t a;
pthread_attr_init(&a);
pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&thid, &a, hot_usb, NULL);
if(ret != 0){
RLOGD("pthread_create error!!!");
return 0;
}
/*lei add for hot usb plug in 2022/8/27*/
/*get pro*/
lynq_init_pms_manager();
g_pmsManagement->lynq_read_pms_from_memory("MPWRSM", &g_lynq_mpwrsm);
g_pmsManagement->lynq_read_pms_from_memory("CSCLK", &g_lynq_csclk);
g_pmsManagement->lynq_read_pms_from_memory("MWAKEUPCFG", &g_lynq_mwakeupcfg);
RLOGD("g_lynq_mpwrsm %d\n", g_lynq_mpwrsm);
//printf("g_lynq_mpwrsm %d\n", g_lynq_mpwrsm);
RLOGD("CSCLK %d\n", g_lynq_csclk);
//printf("CSCLK %d\n", g_lynq_csclk);
RLOGD("MWAKEUPCFG %d\n", g_lynq_mwakeupcfg);
//printf("MWAKEUPCFG %d\n", g_lynq_mwakeupcfg);
int lock_file = open("/tmp/tel_demo_single_proc.lock", O_CREAT|O_RDWR, 0666);
int rc = flock(lock_file,LOCK_EX|LOCK_NB);
if(rc) {
if(EWOULDBLOCK == errno) {
printf("Error: cannot restart the telephony app repeatedly\n");
RLOGD("Error: cannot restart the telephony app repeatedly");
exit(0);
}
}
#ifdef BASELIB_DIR_LIB64
const char *rilLibPath = "/lib64/libvendor-ril.so";
#else
const char *rilLibPath = "/lib/libvendor-ril.so";
#endif
void *dlHandle;
const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
const RIL_RadioFunctions *funcs;
RLOGD("**RIL Daemon Started**");
//prctl(PR_SET_NAME,(unsigned long)"demo_main_thread");
dlHandle = dlopen(rilLibPath, RTLD_NOW);
if (dlHandle == NULL) {
RLOGE("dlopen failed: %s", dlerror());
exit(EXIT_FAILURE);
}
android::RIL_startEventLoop();
//android::startATCILoop();
//android::startPMLoop();
//android::startWakupLoop();
rilInit =
(const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
dlsym(dlHandle, "RIL_Init");
if (rilInit == NULL) {
RLOGE("RIL_Init not defined or exported in %s", rilLibPath);
exit(EXIT_FAILURE);
}
dlerror(); // Clear any previous dlerror
RLOGD("start rilInit");
funcs = rilInit(&s_rilEnv, 0, NULL);;
RLOGD("start RIL_register");
RIL_register(funcs);
android::startGdbusLoop();
RLOGD("RIL_Init RIL_register completed");
printf("DemoApp launch done!\n");
lynq_goto_sleep();
while (true) {
sleep(UINT32_MAX);
}
close(lock_file);
}