Home | History | Annotate | Download | only in cpp
      1 /**
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef MOCK_RIL_REQUESTS_H_
     18 #define MOCK_RIL_REQUESTS_H_
     19 
     20 #include <queue>
     21 #include <pthread.h>
     22 
     23 #include <v8.h>
     24 #include "worker.h"
     25 #include "node_object_wrap.h"
     26 
     27 /**
     28  * A request
     29  */
     30 struct Request {
     31     int request_;
     32     Buffer *buffer_;
     33     RIL_Token token_;
     34 
     35     Request(const int request, const Buffer *buffer, const RIL_Token token) :
     36             request_(0),
     37             buffer_(NULL),
     38             token_(0) {
     39         Set(request, buffer, token);
     40     }
     41 
     42     ~Request() {
     43         delete [] buffer_;
     44     }
     45 
     46     void Set(const int request,
     47         const Buffer *buffer, const RIL_Token token) {
     48         request_ = request;
     49         token_ = token;
     50         buffer_ = (Buffer *)buffer;
     51     }
     52 };
     53 
     54 /**
     55  * Ril request worker queue.
     56  *
     57  * Pass requests to mock-ril.js for processing
     58  */
     59 class RilRequestWorkerQueue : public WorkerQueue {
     60   private:
     61     v8::Handle<v8::Context> context_;
     62     // TODO: Need a thread-safe queue
     63     std::queue<Request *> free_list_;
     64     pthread_mutex_t free_list_mutex_;
     65 
     66   public:
     67     /**
     68      * Constructor
     69      */
     70     RilRequestWorkerQueue(v8::Handle<v8::Context> context);
     71 
     72     /**
     73      * Destructor
     74      */
     75     virtual ~RilRequestWorkerQueue();
     76 
     77     /**
     78      * Add a request to the Queue
     79      */
     80     void AddRequest(const int request,
     81                     const void *data, const size_t datalen, const RIL_Token token);
     82 
     83     /**
     84      * Processes a request sending it to mock-ril.js
     85      */
     86     virtual void Process(void *p);
     87 };
     88 
     89 /**
     90  * Initialize module
     91  *
     92  * @return 0 if no errors
     93  */
     94 int requestsInit(v8::Handle<v8::Context> context, RilRequestWorkerQueue **rwq);
     95 
     96 /**
     97  * Run tests
     98  */
     99 void testRequests(v8::Handle<v8::Context> context);
    100 
    101 #endif  // MOCK_RIL_REQUESTS_H_
    102