Squashed 'LYNQ_PUBLIC/' content from commit 79d8f932f

git-subtree-dir: LYNQ_PUBLIC
git-subtree-split: 79d8f932fb4ebc4b5aec6c5ace97634912394272
Change-Id: If2527ba937f56fe989487bf71e996f7cfd9fbe61
diff --git a/common_src/lib/liblynq-shm/LICENSE b/common_src/lib/liblynq-shm/LICENSE
new file mode 100755
index 0000000..605b7ea
--- /dev/null
+++ b/common_src/lib/liblynq-shm/LICENSE
@@ -0,0 +1,31 @@
+Copyright Statement:
+
+This software/firmware and related documentation ("MobileTek Software") are
+protected under relevant copyright laws. The information contained herein is
+confidential and proprietary to MobileTek Inc. and/or its licensors. Without
+the prior written permission of MobileTek inc. and/or its licensors, any
+reproduction, modification, use or disclosure of MobileTek Software, and
+information contained herein, in whole or in part, shall be strictly
+prohibited.
+
+MobileTek Inc. (C) 2015. All rights reserved.
+
+BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MobileTek SOFTWARE")
+RECEIVED FROM MobileTek AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ON AN "AS-IS" BASIS ONLY. MobileTek EXPRESSLY DISCLAIMS ANY AND ALL
+WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+NONINFRINGEMENT. NEITHER DOES MobileTek PROVIDE ANY WARRANTY WHATSOEVER WITH
+RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+INCORPORATED IN, OR SUPPLIED WITH THE MobileTek SOFTWARE, AND RECEIVER AGREES
+TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MobileTek
+SOFTWARE. MobileTek SHALL ALSO NOT BE RESPONSIBLE FOR ANY MobileTek SOFTWARE
+RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MobileTek'S
+ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MobileTek SOFTWARE
+RELEASED HEREUNDER WILL BE, AT MobileTek'S OPTION, TO REVISE OR REPLACE THE
+MobileTek SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+CHARGE PAID BY RECEIVER TO MobileTek FOR SUCH MobileTek SOFTWARE AT ISSUE.
diff --git a/common_src/lib/liblynq-shm/include/lynq_shm.h b/common_src/lib/liblynq-shm/include/lynq_shm.h
new file mode 100755
index 0000000..bbac100
--- /dev/null
+++ b/common_src/lib/liblynq-shm/include/lynq_shm.h
@@ -0,0 +1,26 @@
+/**

+ * @file shm.h

+ * @author hq

+ * @brief 

+ * @version 1.0

+ * @date 2022-12-16

+ * 

+ * @copyright Copyright (c) 2022

+ * 

+ */

+#ifndef __LYNQ_SHM_H__

+#define __LYNQ_SHM_H__

+

+#ifdef __cplusplus

+extern "C" {

+#endif

+int ril_init_mem();
+void ril_deinit_mem();

+bool get_cur_shem_buffer_index(int size, int* level, int* index);

+bool get_shem_buffer_level(int size, int* level);

+char* get_shem_buffer(int level,int index);

+int get_max_shem_buffer_size();

+#ifdef __cplusplus

+}

+#endif 

+#endif //#ifndef __TEST_SHARED_MEMORY_INCLUDE__

diff --git a/common_src/lib/liblynq-shm/lynq_shm.cpp b/common_src/lib/liblynq-shm/lynq_shm.cpp
new file mode 100755
index 0000000..e06ca96
--- /dev/null
+++ b/common_src/lib/liblynq-shm/lynq_shm.cpp
@@ -0,0 +1,194 @@
+#include <stdio.h>

+#include <stdlib.h>

+#include <string.h>

+//#include <binder/Parcel.h>

+//#include <sys/socket.h>

+#include <errno.h>

+#include <stdbool.h>

+#include <sys/types.h>

+#include <unistd.h>

+#include "lynq_shm.h"

+#include <sys/shm.h>

+#include "log/log.h"

+

+#undef LOG_TAG

+#define LOG_TAG "SHM"

+

+#define shm_key "shm_key"    

+

+int shmid=-1; 

+

+static int s_use_count=0;

+

+typedef struct{

+    int num_of_buffer;

+    int size_of_buffer;

+    int cur_index;

+}lynq_shm_type_config;

+

+typedef enum{

+    shm_buf_type_256,

+    shm_buf_type_1k,

+    shm_buf_type_max,

+}lynq_shm_buf_type;

+

+lynq_shm_type_config s_lynq_shm_config[shm_buf_type_max]={

+    {10,256,0},

+    {5,1024,0}

+};

+

+static void * s_ril_shm_buf=(void*) -1L;

+

+static pthread_mutex_t s_shm_mtx = PTHREAD_MUTEX_INITIALIZER;

+

+bool get_cur_shem_buffer_index(int size, int* level, int* index)

+{

+    pthread_mutex_lock(&s_shm_mtx);

+    for(int i=0;i<shm_buf_type_max;i++)

+    {

+        if(size<=s_lynq_shm_config[i].size_of_buffer)

+        {

+            (*level)=i;

+            (*index)=(s_lynq_shm_config[i].cur_index)%s_lynq_shm_config[i].num_of_buffer;

+            s_lynq_shm_config[i].cur_index++;    

+            pthread_mutex_unlock(&s_shm_mtx);

+            return true;

+        }

+    }    

+    pthread_mutex_unlock(&s_shm_mtx);

+    return false;

+}

+

+bool get_shem_buffer_level(int size, int* level)

+{

+    for(int i=0;i<shm_buf_type_max;i++)

+    {

+        if(size<=s_lynq_shm_config[i].size_of_buffer)

+        {

+            (*level)=i;

+            return true;

+        }

+    }    

+    return false;

+}

+

+

+char* get_shem_buffer(int level,int index)

+{    

+    int offset=0;

+    for(int i=0;i<level;i++)

+    {

+        offset+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;

+    }

+    offset+=s_lynq_shm_config[level].size_of_buffer*index;

+    return ((char*) s_ril_shm_buf)+offset;

+}

+

+int get_max_shem_buffer_size()

+{

+    return s_lynq_shm_config[shm_buf_type_max-1].size_of_buffer;

+}

+

+int get_total_shem_buffer_size()

+{

+    int total_size=0;

+    for(int i=0;i<shm_buf_type_max;i++)

+    {

+        total_size+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;

+    }

+    return total_size;

+}

+

+static int create_shm_common(int size,int flags)

+{

+    RLOGD("create shared memory\n");

+ 

+//  key_t key = ftok(shm_key, 's');

+    key_t key=0x123456;

+    if (key == -1) 

+    {

+        RLOGE("ftok error.\n");

+        return -1;

+    }

+    RLOGD("key is 0x%x\n", key);

+ 

+    int shmid = shmget(key,size , flags);

+    if (shmid == -1) 

+    {

+        RLOGE("shmget error.\n");

+        return -1;

+    }

+    RLOGD("shmid is %d\n", shmid);

+ 

+    return shmid;

+}

+

+int create_shm()

+{

+    shmid = create_shm_common(get_total_shem_buffer_size(),IPC_CREAT|0644);

+

+    if(shmid==-1)

+    {

+        return -1;

+    }

+    s_ril_shm_buf = shmat(shmid, NULL, 0);

+    if (s_ril_shm_buf == (void*) -1L) 

+    {

+        RLOGE("shmat error.\n");

+        return -1;

+    }

+    return 0;    

+}

+ 

+void remove_shm()

+{

+    if(shmid!=-1)

+    {

+        if (shmdt(s_ril_shm_buf) != 0) 

+        {

+            RLOGE("shmdt error.\n");      

+        }        

+        shmid = -1;

+    }

+    s_ril_shm_buf = (void*) -1L;

+    return ;

+}

+

+int ril_init_mem()
+{   

+    pthread_mutex_lock(&s_shm_mtx);

+    RLOGE("init begin, use count is %d.\n",s_use_count);      

+    if(s_use_count==0)

+    {

+        if(create_shm()!=0)

+        {
+            RLOGE("init end, use count is %d.\n",s_use_count);       

+            pthread_mutex_unlock(&s_shm_mtx);

+            return -1;

+        }    

+    }

+    s_use_count++;

+    RLOGE("init end, use count is %d.\n",s_use_count);       

+    pthread_mutex_unlock(&s_shm_mtx);

+    return 0;

+}
+
+void ril_deinit_mem()

+{

+    pthread_mutex_lock(&s_shm_mtx);

+

+    RLOGE("de-init begin, use count is %d.\n",s_use_count);      

+    if(s_use_count==1)

+    {

+        remove_shm();        

+    }

+

+    if(s_use_count>0)

+    {

+        s_use_count--;    

+    }

+    RLOGE("de-init end, use count is %d.\n",s_use_count);      

+    pthread_mutex_unlock(&s_shm_mtx);

+    return ;

+}

+

diff --git a/common_src/lib/liblynq-shm/makefile b/common_src/lib/liblynq-shm/makefile
new file mode 100755
index 0000000..806cc77
--- /dev/null
+++ b/common_src/lib/liblynq-shm/makefile
@@ -0,0 +1,62 @@
+SHELL = /bin/sh
+RM = rm -f
+
+LOCAL_CFLAGS := -Wall \
+                -g -Os \
+                -flto \
+                -fPIC       
+
+PWD := $(shell pwd)
+
+$(warning ################# lynq shm ROOT: $(ROOT),includedir:$(includedir), PWD :$(PWD))
+LOCAL_PATH   = .
+
+LOCAL_C_INCLUDES = \
+  -I. \
+  -I$(LOCAL_PATH)/include \
+  -I$(ROOT)$(includedir) \
+
+LOCAL_LIBS := \
+    -L. \
+    -ldl \
+    -llog \
+#   -lbinder \
+    -lpthread \
+#   -lutils \
+#   -lcutils \
+
+SOURCES = $(wildcard *.cpp)
+
+EXECUTABLE = liblynq-shm.so
+
+OBJECTS=$(SOURCES:.c=.o)
+all: $(EXECUTABLE)
+
+$(EXECUTABLE): $(OBJECTS)
+	$(CXX) -shared -Wl,--no-undefined $(OBJECTS) $(LOCAL_LIBS) $(LOCAL_CFLAGS) $(LOCAL_C_INCLUDES) -o $@
+
+%.o : %.c
+	$(CC) $(LOCAL_C_INCLUDES) $(LOCAL_CFLAGS) $(LOCAL_LIBS) -o $@ -c $<
+
+build:  $(EXECUTABLE)
+	$(warning ########## build $(EXECUTABLE)  ##########)
+
+install:
+	$(warning ################# lynq shm EXECUTABLE: $(EXECUTABLE),base:$(base_libdir))
+	mkdir -p $(ROOT)$(base_libdir)/
+	install $(EXECUTABLE) $(ROOT)$(base_libdir)/
+
+pack_rootfs:
+	$(warning ################# lynq shm PACK: $(PACK_INITRAMFS_TO),base:$(base_libdir))
+	mkdir -p $(PACK_INITRAMFS_TO)$(base_libdir)/
+	cp -af $(EXECUTABLE) $(PACK_INITRAMFS_TO)$(base_libdir)/
+	$(CROSS)strip $(PACK_INITRAMFS_TO)$(base_libdir)/$(EXECUTABLE)
+	mkdir -p $(PACK_TO)$(base_libdir)/
+	cp -af $(EXECUTABLE) $(PACK_TO)$(base_libdir)/
+	$(CROSS)strip $(PACK_TO)$(base_libdir)/$(EXECUTABLE)
+
+.PHONY: clean
+clean:
+	$(RM) $(OBJECTS) $(EXECUTABLE)
+
+