Login | Register
My pages Projects Community openCollabNet

Write your first extension


Table of Contents


  1. Write your first extension
    1. Table of Contents
    2. Prerequisite
    3. Why writing extension was hard
    4. 400 lines equal one line
    5. Memory saved when multple instances are created
    6. Extension demo
    7. How to compile

Prerequisite

Before building your first C++ application, please make sure that the lightblue C++ wrapper class has been properly configured and built. If it is not, please refer this document: How to configure and build lightblue library.

Following files exist in the lightblue directory:
include\brewpp_runtime\AEEimp.hpp
include\brewpp_runtime\brewpp.hpp
lib\vc\entryptr.obj            -- only  needed if you are using the vc compiler
lib\g++\entryptr.o            -- only needed if you are using the GNU ARM compiler
lib\rv22\entryptr.o           -- only needed if you are using the Realview ARM compiler 2.x

You have also have generated mif and bid files.

Why writing extension was hard

Believe it or not, application and extnention are the same thing.  Application is just a special case of extension, which could be more precisely called as " an extension that implemented IApplet interface". However, writing an extension need significantly larger initial code than an application because you have to implement your own code to initialize the extension interface which include following steps:
  1. calculate the size of the memory to be allocate (the size of the instance + size of virtual function table)
  2. initialize the virtual function table
  3. fill the virtual function table with given function pointers
  4. intilize the instance variables
For the IApplet interface, Qualcomm provided a function called AEEApplet_New() in the AEEAppGen.c which contains about 400 lines of source code. For any other custom interfaces, you have to write your own interface initialization  manually,  which  depends on the  number  of  functions  of  the  interface,  might  far exceed  400 lines of  source code.


400 lines equal one line


However, by using lighblue and C++, all  initialization routine equal to one line of C++  code:  pobj =  new Class();  Actually, the C++ new operator and the constructor of the C++ class do the same job as  AEEApplet_New() do to the IApplet interface and  better.

Memory saved when multple instances are created

If you check the implementation of ADDAppGen(), you will find that virtual function table  is created per instance not per class. It will waste memory when multiple instances are created for the same extension.  The C++ will always guarrantee that  only one virtual function table is created for each class, thus less memory is consumed when multple isntances are created.


Extension demo

By using lighblue, there is not different between creating an application and an extension. The only different is that an extension can be inherited from any base interfaces while the application can only derived from IApplet.

#include "extension.h"
#include "brewpp_runtime/brewpp.hpp"
#include "AEEStdLib.h"

class CTestExtensionImp:public CBaseImp<IBrewTestExtension>
{
public:
    CTestExtensionImp(CDefaultBrewModule* pmod)
        :m_pIModule(pmod)
    {
        if(m_pIModule)
        {
            m_pIModule->AddRef();
        }
        STRTOWSTR("!!! Hello extension !!!",m_Msg,sizeof(m_Msg));
    }

    override const AECHAR* BREW_CALL_CONV getString()
    {
        return m_Msg;
    }

    override ~CTestExtensionImp()
    {
        if(m_pIModule)
        {
            m_pIModule->Release();
        }
    }

private:
    CDefaultBrewModule* m_pIModule;
    AECHAR m_Msg[32];
};

extern "C" int AEEClsCreateInstance(AEECLSID ClsId,CDefaultBrewModule *pMod, void **ppobj)
{
    *ppobj = NULL;
    if(ClsId == CTestExtensionImp::CLS_ID)
    {
        *ppobj = (void**)new CTestExtensionImp(pMod);
        return(AEE_SUCCESS);
    }
    return EFAILED;
}


How to compile

There is no different between build an extension and build an application.