Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/* Copyright (C) 2010-2011 Circuits At Home, LTD. All rights reserved.
2
 
3
This software may be distributed and modified under the terms of the GNU
4
General Public License version 2 (GPL2) as published by the Free Software
5
Foundation and appearing in the file GPL2.TXT included in the packaging of
6
this file. Please note that GPL2 Section 2[b] requires that all works based
7
on this software must also be made publicly available under the terms of
8
the GPL2 ("Copyleft").
9
 
10
Contact information
11
-------------------
12
 
13
Circuits At Home, LTD
14
Web      :  http://www.circuitsathome.com
15
e-mail   :  support@circuitsathome.com
16
*/
17
#ifndef __PTP_H__
18
#define __PTP_H__
19
 
20
#define PTPDEBUG
21
 
22
#include <inttypes.h>
23
#include <avr/pgmspace.h>
24
#include <Max3421e.h>
25
#include <Usb.h>
26
#include "ptpconst.h"
27
#include "ptpmsgstr.h"
28
#include "ptpcallback.h"
29
#include "ptpdebug.h"
30
 
31
// Buffer size should NEVER be less than USB packet size!!!!!!!!!!!!!!!!!!!!!
32
#define PTP_MAX_RX_BUFFER_LEN   64
33
#define PTP_MAX_EV_BUFFER_LEN    8
34
 
35
typedef void (*PTPMAIN)();
36
 
37
// States
38
#define PTP_STATE_DEVICE_DISCONNECTED           1
39
#define PTP_STATE_SESSION_NOT_OPENED            2
40
#define PTP_STATE_SESSION_OPENED                        3
41
#define PTP_STATE_DEVICE_INITIALIZED            4
42
#define PTP_STATE_DEVICE_NOT_RESPONDING         5
43
#define PTP_STATE_DEVICE_BUSY                           6
44
 
45
#define FAILED(rc)(rc != PTP_RC_OK)
46
 
47
class PTP;
48
 
49
class PTPStateHandlers
50
{
51
public:
52
        virtual void OnDeviceDisconnectedState(PTP *ptp);
53
        virtual void OnSessionNotOpenedState(PTP *ptp);
54
        virtual void OnSessionOpenedState(PTP *ptp);
55
        virtual void OnDeviceInitializedState(PTP *ptp);
56
        virtual void OnDeviceNotRespondingState(PTP *ptp);
57
        virtual void OnDeviceBusyState(PTP *ptp);
58
};
59
 
60
class PTP
61
{
62
        uint8_t         theState;
63
        uint8_t         busyTime;
64
        uint8_t         hangTime;
65
 
66
protected:
67
        void SetInitialState();
68
        void Task2();
69
 
70
private:
71
        uint8_t                         devAddress;                             // Device address
72
        uint8_t                         epDataIn;                               // DataIN  endpoint number
73
        uint8_t                         epDataOut;                              // DataOUT endpoint number
74
        uint8_t                         epInterrupt;                    // Interrupt endpoint number
75
        uint8_t                         numConf;                                // Number of the configuration
76
 
77
        MAX3421E                        Max;
78
        USB                                     Usb;
79
 
80
        uint16_t                        idTransaction;                  // Transaction ID
81
        uint16_t                        idSession;                              // Session ID
82
 
83
        PTPStateHandlers        *stateMachine;
84
 
85
protected:
86
        EP_RECORD                       epRecord[ 4 ];
87
 
88
        struct OperFlags
89
        {
90
                uint16_t        opParams        :       3;                      // 7    - maximum number of operation parameters
91
                uint16_t        rsParams        :       3;                      // 7    - maximum number of response parameters
92
                uint16_t        txOperation     :       1;                      // I->R operation if the flag is set
93
                uint16_t        dataStage       :       1;                      // operation has data stage if the flag is set
94
                uint16_t        typeOfVoid      :       2;                      // 0 - NULL, 1 - PTPReadParser/PTPDataSupplyer, 2 - WRITEPARSER, 3 - buffer pointer
95
                uint16_t        dataSize        :       6;                      // size of data buffer (64 bytes maximum)               
96
        };
97
        typedef void (*READPARSER)(const uint16_t len, const uint8_t *pbuf, const uint32_t &offset);
98
 
99
        void ZerroMemory(uint8_t size, uint8_t *mem) { for (uint8_t i=0; i<size; i++) mem[i] = 0; };
100
 
101
        // waits for any event to occure
102
        // returns event on success or error code if timeout elapsed
103
        bool EventWait(uint8_t size, uint8_t *event_buf, uint16_t timeout);
104
 
105
        // returns true if event occured
106
        // the actual data is stored in a buffer pointed by buf
107
        bool CheckEvent(uint8_t size, uint8_t *buf);
108
 
109
        uint16_t Transaction(uint16_t opcode, OperFlags *flags, uint32_t *params, void *pVoid);
110
 
111
        void HaltOnError(const char* msg, uint16_t rcode) { Message(msg, rcode); while(1); };
112
 
113
public:
114
        PTP(uint8_t addr, uint8_t epin, uint8_t epout, uint8_t epint, uint8_t nconf, PTPStateHandlers *s);
115
 
116
        MAX3421E* GetMax() { return &Max; };
117
        USB* GetUsb() { return &Usb; };
118
 
119
        void SetState(uint8_t state) { theState = state; };
120
        uint8_t GetState() { return theState; };
121
 
122
        virtual uint16_t EventCheck(PTPReadParser *parser);
123
 
124
        void    Setup() { Max.powerOn(); };
125
        void    Task();
126
 
127
        // Simple PTP operation which has no data stage. Any number of uint32_t params can be supplied.
128
        uint16_t Operation(uint16_t opcode, uint8_t nparams = 0, uint32_t *params = NULL);
129
 
130
        uint16_t CaptureImage();
131
 
132
        uint16_t OpenSession();
133
        uint16_t CloseSession();
134
        uint16_t ResetDevice();
135
        uint16_t PowerDown();
136
        uint16_t SelfTest(uint16_t type);
137
        uint16_t GetDeviceInfo(PTPReadParser *parser);
138
        uint16_t GetDevicePropDesc(const uint16_t pcode, PTPReadParser *parser);
139
        uint16_t GetDevicePropValue(const uint16_t pcode, PTPReadParser *parser);
140
        uint16_t GetDevicePropValue(const uint16_t pcode, uint8_t  &val);
141
        uint16_t GetDevicePropValue(const uint16_t pcode, uint16_t &val);
142
        uint16_t GetDevicePropValue(const uint16_t pcode, uint32_t &val);
143
        uint16_t SetDevicePropValue(const uint16_t pcode, uint8_t  val);
144
        uint16_t SetDevicePropValue(const uint16_t pcode, uint16_t val);
145
        uint16_t SetDevicePropValue(const uint16_t pcode, uint32_t val);
146
        uint16_t ResetDevicePropValue(const uint16_t pcode);
147
        uint16_t GetStorageIDs(PTPReadParser *parser);
148
        uint16_t GetStorageIDs(uint8_t bufsize, uint8_t *pbuf);
149
        uint16_t GetStorageInfo(uint32_t storage_id, PTPReadParser *parser);
150
        uint16_t GetObjectHandles(uint32_t storage_id, uint16_t format, uint16_t assoc, PTPReadParser *parser);
151
        uint16_t GetObjectInfo(uint32_t handle, PTPReadParser *parser);
152
        uint16_t FormatStore(uint32_t storage_id, uint32_t fsformat);
153
        uint16_t GetObject(uint32_t handle, PTPReadParser *parser);
154
        uint16_t GetThumb(uint32_t handle, PTPReadParser *parser);
155
 
156
        uint16_t GetNumObjects(uint32_t &retval, uint32_t storage_id = 0xffffffff, uint16_t format = 0, uint32_t assoc = 0);
157
        uint16_t DeleteObject(uint32_t handle, uint16_t format = 0);
158
        uint16_t SetObjectProtection(uint32_t handle, uint16_t attrib);
159
        uint16_t MoveObject(uint32_t handle, uint32_t storage_id, uint32_t parent);
160
        uint16_t CopyObject(uint32_t handle, uint32_t storage_id, uint32_t parent, uint32_t &new_handle);
161
        uint16_t InitiateOpenCapture(uint32_t storage_id = 0, uint16_t format = 0);
162
        uint16_t TerminateOpenCapture(uint32_t trans_id);
163
        uint16_t InitiateCapture(uint32_t storage_id = 0, uint16_t format = 0);
164
 
165
        // To be implemented in future releases
166
        //uint16_t GetPartialObject(uint32_t handle, PTPReadParser *parser);
167
        //uint16_t SendObjectInfo(uint32_t handle, PTPDataSupplier *sup);
168
        //uint16_t SendObject(uint32_t handle, PTPDataSupplier *sup);
169
 
170
private:
171
        uint8_t GetConfDescr( byte addr, byte conf );
172
};
173
 
174
 
175
#if defined( PTPDEBUG )
176
#define PTPTRACE(s)(Notify(PSTR((s))))
177
#define PTPTRACE2(s,r)(Message(PSTR((s)),(r)));
178
#else
179
#define PTPTRACE(s)((void)0)
180
#define PTPTRACE2(s,r)(delay(1))        // necessary for some PowerShot cameras to work properly
181
#endif
182
 
183
#endif // __PTP_H__