jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.9KB

  1. #define LOG_TAG "JAMSHMSERVICE"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <binder/MemoryHeapBase.h>
  8. #include <binder/IServiceManager.h>
  9. #include <binder/IPCThreadState.h>
  10. #include <utils/Log.h>
  11. #include <sys/stat.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <sys/un.h>
  15. #include <sys/wait.h>
  16. #include "BnAndroidShm.h"
  17. #include "AndroidShm.h"
  18. #include "JackConstants.h"
  19. #include <fcntl.h>
  20. #include <signal.h>
  21. #include <limits.h>
  22. #include <errno.h>
  23. #include <dirent.h>
  24. #include <sys/mman.h>
  25. #include <linux/ashmem.h>
  26. #include <cutils/ashmem.h>
  27. #include "JackError.h"
  28. #include <semaphore.h>
  29. #define MEMORY_SIZE 10*1024
  30. #define SEMAPHORE_NULL_CHAR '\0'
  31. // remove ALOGI log
  32. #undef ALOGI
  33. #define ALOGI
  34. namespace android {
  35. int AndroidShm::instantiate() {
  36. defaultServiceManager()->addService(String16("com.samsung.android.jam.IAndroidShm"), new AndroidShm); // SINGLETON WITH SAME NAME
  37. return 0;
  38. }
  39. int AndroidShm::sendCommand(const char* command) {
  40. ALOGI("I(pid:%d) send command is %s\n", getpid(), command);
  41. if(strcmp(command, "semaphore") == 0) {
  42. // print debug message about semaphore simulation
  43. for(int i = MAX_SEMAPHORE_MEMORY_COUNT -1 ; i >= 0; i--) {
  44. printf("index[%3d] = ptr[%p] name[%s]\n", i, (mSemaphore[i] != NULL)?mSemaphore[i]->getBase():0, mSemaphoreName[i]);
  45. ALOGI("index[%3d] = ptr[%p] name[%s]\n", i, (mSemaphore[i] != NULL)?mSemaphore[i]->getBase():0, mSemaphoreName[i]);
  46. }
  47. }
  48. return NO_ERROR;
  49. }
  50. int AndroidShm::testGetBufferByNewProcess() {
  51. ALOGI("testGetBufferByNewProcess...");
  52. int status;
  53. int childPid = fork();
  54. if(childPid > 0) {
  55. ALOGI("I(pid%d) made a child process(pid:%d)", getpid(), childPid);
  56. ALOGI("I(pid%d) wait until child(%d) was finish", getpid(), childPid);
  57. wait(&status);
  58. // wait 하지 않으면 child process가 남아 있음.
  59. ALOGI("child(%d) was finished. ", childPid);
  60. } else if(childPid == 0) {
  61. ALOGI("im a new child process(pid:%d) ", getpid());
  62. if(-1 == execlp("/system/bin/getbufferclient","getbufferclient",NULL)) {
  63. ALOGE("failed to execute getbufferclient");
  64. }
  65. exit(0);
  66. } else {
  67. ALOGI("failed creating child process");
  68. }
  69. return 0;
  70. }
  71. int AndroidShm::testGetBuffer() {
  72. ALOGI("I(pid:%d) trying to test get buffer...", getpid());
  73. sp<IServiceManager> sm = defaultServiceManager();
  74. ALOGI("get default ServiceManager is done");
  75. sp<IBinder> b;
  76. //String16* serviceName = new String16("com.samsung.android.jam.IAndroidShm");
  77. do {
  78. //ALOGI("here");
  79. b = sm->getService(String16("com.samsung.android.jam.IAndroidShm"));
  80. //ALOGI("getservice is done");
  81. if(b != 0)
  82. break;
  83. //ALOGI("AndroidShm is not working, waiting...");
  84. usleep(500000);
  85. } while(true);
  86. sp<IAndroidShm> service = interface_cast<IAndroidShm>(b);
  87. //shared buffer.
  88. sp<IMemoryHeap> receiverMemBase = service->getBuffer(0);
  89. unsigned int *base = (unsigned int *) receiverMemBase->getBase();
  90. int ret = 0;
  91. if(base != (unsigned int *) -1) {
  92. ALOGD("AndroidShm::testGetBuffer base=%p Data=0x%x\n",base, *base);
  93. *base = (*base)+1;
  94. ret = (unsigned int)(*base);
  95. ALOGI("AndroidShm::testGetBuffer base=%p Data=0x%x CHANGED\n",base, *base);
  96. receiverMemBase = 0;
  97. } else {
  98. ALOGE("Error shared memory not available\n");
  99. }
  100. return 0;
  101. }
  102. sp<IMemoryHeap> AndroidShm::getBuffer(int index) {
  103. ALOGI("I(pid:%d) getBuffer index:%d", getpid(), index);
  104. if(index < 0 || index >= MAX_SHARED_MEMORY_COUNT) {
  105. ALOGE("error : out of index [%d]", index);
  106. return NULL;
  107. }
  108. return mMemHeap[index];
  109. }
  110. int AndroidShm::MemAlloc(unsigned int size) {
  111. ALOGI("try to allocate memory size[%d]", size);
  112. for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
  113. if(mMemHeap[i] == NULL) {
  114. mMemHeap[i] = new MemoryHeapBase(size);
  115. if(mMemHeap[i] == NULL){
  116. ALOGI("fail to alloc, try one more...");
  117. continue; // try one more.
  118. }
  119. return i;
  120. }
  121. }
  122. ALOGE("fail to MemAlloc");
  123. return -1; // fail to alloc
  124. }
  125. AndroidShm::AndroidShm() {
  126. ALOGI("AndroidShm is created");
  127. for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
  128. mMemHeap[i] = NULL;
  129. }
  130. mRegistryIndex = 10000;
  131. //mMemHeap = new MemoryHeapBase(MEMORY_SIZE);
  132. //unsigned int *base = (unsigned int*) mMemHeap->getBase();
  133. //*base = 0xdeadcafe;//
  134. for(int j = 0; j < MAX_SEMAPHORE_MEMORY_COUNT; j++) {
  135. mSemaphore[j] = NULL;
  136. memset(mSemaphoreName[j], SEMAPHORE_NULL_CHAR, MAX_SEMAPHORE_NAME_LENGTH);
  137. }
  138. }
  139. AndroidShm::~AndroidShm() {
  140. ALOGI("AndroidShm is destroyed");
  141. for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
  142. (mMemHeap[i]).clear();
  143. }
  144. for(int j = 0; j < MAX_SEMAPHORE_MEMORY_COUNT; j++) {
  145. (mSemaphore[j]).clear();
  146. }
  147. }
  148. //status_t AndroidShm::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
  149. // return BnAndroidShm::onTransact(code, data, reply, flags);
  150. //}
  151. int AndroidShm::allocShm(const int size) { // if negative return value is error
  152. ALOGI("try to alloc shared memory size[%d]", size);
  153. return MemAlloc(size);
  154. }
  155. int AndroidShm::removeShm(const unsigned int index) { // shared memory 제거
  156. ALOGI("try to remove shared memory index[%d]", index);
  157. if(index >= MAX_SHARED_MEMORY_COUNT) {
  158. ALOGE("remove shared memory: out of index");
  159. return -1;
  160. }
  161. (mMemHeap[index]).clear();
  162. return 1;
  163. }
  164. int AndroidShm::isAllocated(const unsigned int index) { // allocated 여부 확인
  165. ALOGI("try to check the memory allocation index[%d]", index);
  166. if(index >= MAX_SHARED_MEMORY_COUNT) {
  167. ALOGE("shared memory: out of index");
  168. return 0;
  169. }
  170. if(mMemHeap[index] == NULL)
  171. return 0;
  172. else
  173. return 1;
  174. }
  175. int AndroidShm::setRegistryIndex(const unsigned int index) {
  176. ALOGI("set registry index %d", index);
  177. mRegistryIndex = index;
  178. return 1;
  179. }
  180. int AndroidShm::getRegistryIndex() {
  181. return mRegistryIndex;
  182. }
  183. sp<IMemoryHeap> AndroidShm::InitSemaphore(const char* name) {
  184. ALOGI("init semaphore [%s]", name);
  185. for(int i = 0; i < MAX_SEMAPHORE_MEMORY_COUNT; i++) {
  186. if(mSemaphoreName[i][0] == SEMAPHORE_NULL_CHAR) {
  187. mSemaphore[i] = new MemoryHeapBase(sizeof(sem_t));
  188. if(mSemaphore[i] == NULL){
  189. ALOGI("fail to alloc, try one more...");
  190. continue;
  191. }
  192. if(sem_init((sem_t*)(mSemaphore[i]->getBase()), 1, 0) == 0) {
  193. strncpy(mSemaphoreName[i], name, MAX_SEMAPHORE_NAME_LENGTH - 1);
  194. mSemaphoreName[i][MAX_SEMAPHORE_NAME_LENGTH - 1] = '\0';
  195. ALOGI("sem_init success");
  196. return mSemaphore[i];
  197. } else {
  198. (mSemaphore[i]).clear();
  199. ALOGE("sem_init failed null returned");
  200. return NULL;
  201. }
  202. } else {
  203. // find already exist name
  204. if(strcmp(mSemaphoreName[i], name) == 0) { // found
  205. ALOGI("found - return alread allocated semaphore");
  206. return mSemaphore[i];
  207. }
  208. }
  209. }
  210. ALOGE("sem_init failed null returned 2");
  211. return NULL;
  212. }
  213. };